diff --git a/src/frontend/api.rs b/src/frontend/api.rs index c58c8a7..546302b 100644 --- a/src/frontend/api.rs +++ b/src/frontend/api.rs @@ -4,7 +4,7 @@ use crate::common::{ *, }; use http::{Method, StatusCode}; -use leptos::prelude::ServerFnError; +use leptos::{prelude::ServerFnError, server_fn::error::NoCustomError}; use log::error; use newtypes::PersonId; use serde::{Deserialize, Serialize}; @@ -22,7 +22,7 @@ pub static CLIENT: LazyLock = LazyLock::new(|| { } }); -#[derive(Clone)] +#[derive(Clone, Debug)] pub struct ApiClient { #[cfg(feature = "ssr")] client: reqwest::Client, @@ -277,8 +277,9 @@ impl ApiClient { } let res = req.send().await?; let status = res.status(); + let url = res.url().to_string(); let text = res.text().await?.to_string(); - Self::response(status.into(), text) + Self::response(status.into(), text, &url) } #[cfg(not(feature = "ssr"))] @@ -333,19 +334,25 @@ impl ApiClient { let res = req.send().await?; let status = res.status(); let text = res.text().await?; - Self::response(status, text) + Self::response(status, text, &res.url()) }) } - fn response(status: u16, text: String) -> Result + fn response(status: u16, text: String, url: &str) -> Result where T: for<'de> Deserialize<'de>, { - let json = serde_json::from_str(&text)?; + let json = serde_json::from_str(&text).map_err(|e| { + ServerFnError::::Deserialization(format!( + "Serde error: {e} from {text}on {url}" + )) + })?; if status == StatusCode::OK { Ok(json) } else { - Err(ServerFnError::Response(format!("API error: {text}"))) + Err(ServerFnError::Response(format!( + "API error: {text} on {url} status {status}" + ))) } }