From 1d19b8c2a6194e0fca5f50c4721a34d79c77c037 Mon Sep 17 00:00:00 2001 From: alemi Date: Sat, 11 May 2024 17:58:01 +0200 Subject: [PATCH] feat(web): debug fetch shows error text from remote --- web/src/lib.rs | 8 ++++---- web/src/page.rs | 21 +++++++++++++++------ 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/web/src/lib.rs b/web/src/lib.rs index 5d49657a..02cdab85 100644 --- a/web/src/lib.rs +++ b/web/src/lib.rs @@ -98,21 +98,21 @@ impl Http { req = req.json(data); } - req.send() - .await? - .error_for_status() + req.send().await } pub async fn fetch(url: &str, token: Auth) -> reqwest::Result { Self::request::<()>(reqwest::Method::GET, url, None, token) .await? + .error_for_status()? .json::() .await } pub async fn post(url: &str, data: &T, token: Auth) -> reqwest::Result<()> { Self::request(reqwest::Method::POST, url, Some(data), token) - .await?; + .await? + .error_for_status()?; Ok(()) } } diff --git a/web/src/page.rs b/web/src/page.rs index 42b924cb..57b2fa8b 100644 --- a/web/src/page.rs +++ b/web/src/page.rs @@ -299,12 +299,7 @@ pub fn DebugPage() -> impl IntoView { } } else { let url = format!("{URL_BASE}/dbg?id={fetch_url}"); - spawn_local(async move { - match Http::fetch::(&url, auth).await { - Ok(x) => set_object.set(Arc::new(x)), - Err(e) => set_object.set(Arc::new(serde_json::Value::String(e.to_string()))), - } - }); + spawn_local(async move { set_object.set(Arc::new(debug_fetch(&url, auth).await)) }); } } > @@ -385,3 +380,17 @@ pub fn SearchPage() -> impl IntoView { } } + +// this is a rather weird way to fetch but i want to see the bare error text if it fails! +async fn debug_fetch(url: &str, token: Auth) -> serde_json::Value { + match Http::request::<()>(reqwest::Method::GET, url, None, token).await { + Err(e) => serde_json::Value::String(format!("[!] failed sending request: {e}")), + Ok(res) => match res.text().await { + Err(e) => serde_json::Value::String(format!("[!] invalid response body: {e}")), + Ok(x) => match serde_json::from_str(&x) { + Err(_) => serde_json::Value::String(x), + Ok(v) => v, + }, + } + } +}