mirror of
https://github.com/Nutomic/ibis.git
synced 2024-12-23 20:11:24 +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 reqwest::{Client, RequestBuilder};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use crate::frontend::error::MyResult;
|
||||
|
||||
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 };
|
||||
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
|
||||
T: for<'de> Deserialize<'de>,
|
||||
R: Serialize,
|
||||
|
@ -24,23 +25,23 @@ where
|
|||
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
|
||||
T: for<'de> Deserialize<'de>,
|
||||
{
|
||||
let res = req.send().await.unwrap();
|
||||
let res = req.send().await?;
|
||||
let status = res.status();
|
||||
let text = res.text().await.unwrap();
|
||||
let text = res.text().await?;
|
||||
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}"))
|
||||
.unwrap()
|
||||
?)
|
||||
} 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 {
|
||||
username: username.to_string(),
|
||||
password: password.to_string(),
|
||||
|
@ -55,7 +56,7 @@ pub async fn login(
|
|||
hostname: &str,
|
||||
username: &str,
|
||||
password: &str,
|
||||
) -> LoginResponse {
|
||||
) -> MyResult<LoginResponse> {
|
||||
let login_form = LoginUserData {
|
||||
username: username.to_string(),
|
||||
password: password.to_string(),
|
||||
|
|
|
@ -13,7 +13,7 @@ pub fn Article() -> impl IntoView {
|
|||
.cloned()
|
||||
.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! {
|
||||
|
|
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(
|
||||
async move {
|
||||
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;
|
||||
mod login;
|
||||
pub mod nav;
|
||||
mod error;
|
||||
|
||||
#[cfg(feature = "hydrate")]
|
||||
#[wasm_bindgen::prelude::wasm_bindgen]
|
||||
|
|
Loading…
Reference in a new issue