mirror of
https://github.com/Nutomic/ibis.git
synced 2024-11-22 14:31:08 +00:00
bit of error handling
This commit is contained in:
parent
b6c1512479
commit
90d92f5391
5 changed files with 35 additions and 12 deletions
|
@ -4,15 +4,16 @@ use anyhow::anyhow;
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use reqwest::{Client, RequestBuilder};
|
use reqwest::{Client, RequestBuilder};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
use crate::frontend::error::MyResult;
|
||||||
|
|
||||||
pub static CLIENT: Lazy<Client> = Lazy::new(Client::new);
|
pub static CLIENT: Lazy<Client> = Lazy::new(Client::new);
|
||||||
|
|
||||||
pub async fn get_article(hostname: &str, title: String) -> ArticleView {
|
pub async fn get_article(hostname: &str, title: String) -> MyResult<ArticleView> {
|
||||||
let get_article = GetArticleData { title };
|
let get_article = GetArticleData { title };
|
||||||
get_query::<ArticleView, _>(hostname, "article", Some(get_article.clone())).await
|
get_query::<ArticleView, _>(hostname, "article", Some(get_article.clone())).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_query<T, R>(hostname: &str, endpoint: &str, query: Option<R>) -> T
|
pub async fn get_query<T, R>(hostname: &str, endpoint: &str, query: Option<R>) -> MyResult<T>
|
||||||
where
|
where
|
||||||
T: for<'de> Deserialize<'de>,
|
T: for<'de> Deserialize<'de>,
|
||||||
R: Serialize,
|
R: Serialize,
|
||||||
|
@ -24,23 +25,23 @@ where
|
||||||
handle_json_res::<T>(req).await
|
handle_json_res::<T>(req).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn handle_json_res<T>(req: RequestBuilder) -> T
|
pub async fn handle_json_res<T>(req: RequestBuilder) -> MyResult<T>
|
||||||
where
|
where
|
||||||
T: for<'de> Deserialize<'de>,
|
T: for<'de> Deserialize<'de>,
|
||||||
{
|
{
|
||||||
let res = req.send().await.unwrap();
|
let res = req.send().await?;
|
||||||
let status = res.status();
|
let status = res.status();
|
||||||
let text = res.text().await.unwrap();
|
let text = res.text().await?;
|
||||||
if status == reqwest::StatusCode::OK {
|
if status == reqwest::StatusCode::OK {
|
||||||
serde_json::from_str(&text)
|
Ok(serde_json::from_str(&text)
|
||||||
.map_err(|e| anyhow!("Json error on {text}: {e}"))
|
.map_err(|e| anyhow!("Json error on {text}: {e}"))
|
||||||
.unwrap()
|
?)
|
||||||
} else {
|
} else {
|
||||||
Err(anyhow!("API error: {text}")).unwrap()
|
Err(anyhow!("API error: {text}").into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn register(hostname: &str, username: &str, password: &str) -> LoginResponse {
|
pub async fn register(hostname: &str, username: &str, password: &str) -> MyResult<LoginResponse> {
|
||||||
let register_form = RegisterUserData {
|
let register_form = RegisterUserData {
|
||||||
username: username.to_string(),
|
username: username.to_string(),
|
||||||
password: password.to_string(),
|
password: password.to_string(),
|
||||||
|
@ -55,7 +56,7 @@ pub async fn login(
|
||||||
hostname: &str,
|
hostname: &str,
|
||||||
username: &str,
|
username: &str,
|
||||||
password: &str,
|
password: &str,
|
||||||
) -> LoginResponse {
|
) -> MyResult<LoginResponse> {
|
||||||
let login_form = LoginUserData {
|
let login_form = LoginUserData {
|
||||||
username: username.to_string(),
|
username: username.to_string(),
|
||||||
password: password.to_string(),
|
password: password.to_string(),
|
||||||
|
|
|
@ -13,7 +13,7 @@ pub fn Article() -> impl IntoView {
|
||||||
.cloned()
|
.cloned()
|
||||||
.unwrap_or("Main Page".to_string())
|
.unwrap_or("Main Page".to_string())
|
||||||
},
|
},
|
||||||
move |title| async move { get_article("localhost:8131", title).await },
|
move |title| async move { get_article("localhost:8131", title).await.unwrap() },
|
||||||
);
|
);
|
||||||
|
|
||||||
view! {
|
view! {
|
||||||
|
|
21
src/frontend/error.rs
Normal file
21
src/frontend/error.rs
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
use std::fmt::{Display, Formatter};
|
||||||
|
|
||||||
|
pub type MyResult<T> = Result<T, Error>;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Error(pub anyhow::Error);
|
||||||
|
|
||||||
|
impl Display for Error {
|
||||||
|
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||||
|
std::fmt::Display::fmt(&self.0, f)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> From<T> for Error
|
||||||
|
where
|
||||||
|
T: Into<anyhow::Error>,
|
||||||
|
{
|
||||||
|
fn from(t: T) -> Self {
|
||||||
|
Error(t.into())
|
||||||
|
}
|
||||||
|
}
|
|
@ -11,7 +11,7 @@ fn do_login(ev: SubmitEvent, username: String, password: String) {
|
||||||
spawn_local(
|
spawn_local(
|
||||||
async move {
|
async move {
|
||||||
let res = login("localhost:8080", &username, &password).await;
|
let res = login("localhost:8080", &username, &password).await;
|
||||||
info!("{}", res.jwt);
|
info!("{}", res.unwrap().jwt);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ pub mod app;
|
||||||
pub mod article;
|
pub mod article;
|
||||||
mod login;
|
mod login;
|
||||||
pub mod nav;
|
pub mod nav;
|
||||||
|
mod error;
|
||||||
|
|
||||||
#[cfg(feature = "hydrate")]
|
#[cfg(feature = "hydrate")]
|
||||||
#[wasm_bindgen::prelude::wasm_bindgen]
|
#[wasm_bindgen::prelude::wasm_bindgen]
|
||||||
|
|
Loading…
Reference in a new issue