feat(web): debug fetch shows error text from remote

This commit is contained in:
əlemi 2024-05-11 17:58:01 +02:00
parent e9fe8ba236
commit 1d19b8c2a6
Signed by: alemi
GPG key ID: A4895B84D311642C
2 changed files with 19 additions and 10 deletions

View file

@ -98,21 +98,21 @@ impl Http {
req = req.json(data);
}
req.send()
.await?
.error_for_status()
req.send().await
}
pub async fn fetch<T: serde::de::DeserializeOwned>(url: &str, token: Auth) -> reqwest::Result<T> {
Self::request::<()>(reqwest::Method::GET, url, None, token)
.await?
.error_for_status()?
.json::<T>()
.await
}
pub async fn post<T: serde::ser::Serialize>(url: &str, data: &T, token: Auth) -> reqwest::Result<()> {
Self::request(reqwest::Method::POST, url, Some(data), token)
.await?;
.await?
.error_for_status()?;
Ok(())
}
}

View file

@ -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::<serde_json::Value>(&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)) });
}
} >
<table class="align w-100" >
@ -385,3 +380,17 @@ pub fn SearchPage() -> impl IntoView {
</blockquote>
}
}
// 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,
},
}
}
}