use std::convert::Infallible; use std::ops::FromResidual; use seed::fetch::{FetchError, Request}; pub mod admin; pub mod public; #[derive(Debug)] pub enum NetRes { Success(S), Error(model::api::Failure), Http(FetchError), } impl From for NetRes { fn from(e: FetchError) -> Self { Self::Http(e) } } impl FromResidual>> for NetRes { fn from_residual(residual: Result>) -> Self { match residual { Ok(_s) => unreachable!(), Err(s) => s, } } } async fn perform(req: Request<'_>) -> NetRes { let res = match req.fetch().await { Ok(res) => res, Err(err) => return NetRes::Http(err), }; if res.status().is_error() { NetRes::Error(match res.json().await { Ok(json) => json, Err(_err) => match res.text().await { Ok(text) => model::api::Failure { errors: vec![text] }, Err(_err) => model::api::Failure { errors: vec!["There was internal server error. Please try later".into()], }, }, }) } else { match res.json().await { Ok(json) => NetRes::Success(json), Err(err) => NetRes::Http(err), } } }