From 793b78dbb1d9d7fd25eacd8cb665360d874eaf62 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sun, 31 Dec 2023 14:01:44 +0000 Subject: [PATCH] Report HTTP errors from all URLs we retrieve. --- src/client.rs | 63 ++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 18 deletions(-) diff --git a/src/client.rs b/src/client.rs index da6fdd5..0a7cdfa 100644 --- a/src/client.rs +++ b/src/client.rs @@ -224,10 +224,16 @@ impl Client { let (url, req) = self.api_request(Req::get( &("accounts/".to_owned() + id)))?; - let body = req.send()?.text()?; - let ac: Account = match serde_json::from_str(&body) { - Ok(ac) => Ok(ac), - Err(e) => Err(ClientError::UrlError(url.clone(), e.to_string())), + let rsp = req.send()?; + let rspstatus = rsp.status(); + let ac: Account = if !rspstatus.is_success() { + Err(ClientError::UrlError(url.clone(), rspstatus.to_string())) + } else { + match serde_json::from_str(&rsp.text()?) { + Ok(ac) => Ok(ac), + Err(e) => Err(ClientError::UrlError( + url.clone(), e.to_string())), + } }?; if ac.id != id { return Err(ClientError::UrlError( @@ -251,12 +257,17 @@ impl Client { let (url, req) = self.api_request(Req::get( &("statuses/".to_owned() + id)))?; - let body = req.send()?.text()?; - let st: Status = match serde_json::from_str(&body) { - Ok(st) => Ok(st), - Err(e) => { - Err(ClientError::UrlError(url.clone(), e.to_string())) - }, + let rsp = req.send()?; + let rspstatus = rsp.status(); + let st: Status = if !rspstatus.is_success() { + Err(ClientError::UrlError(url.clone(), rspstatus.to_string())) + } else { + match serde_json::from_str(&rsp.text()?) { + Ok(st) => Ok(st), + Err(e) => { + Err(ClientError::UrlError(url.clone(), e.to_string())) + }, + } }?; if st.id != id { return Err(ClientError::UrlError( @@ -291,12 +302,17 @@ impl Client { let (url, req) = self.api_request(Req::get( &("notifications/".to_owned() + id)))?; - let body = req.send()?.text()?; - let not: Notification = match serde_json::from_str(&body) { - Ok(st) => Ok(st), - Err(e) => { - Err(ClientError::UrlError(url.clone(), e.to_string())) - }, + let rsp = req.send()?; + let rspstatus = rsp.status(); + let not: Notification = if !rspstatus.is_success() { + Err(ClientError::UrlError(url.clone(), rspstatus.to_string())) + } else { + match serde_json::from_str(&rsp.text()?) { + Ok(st) => Ok(st), + Err(e) => { + Err(ClientError::UrlError(url.clone(), e.to_string())) + }, + } }?; if not.id != id { return Err(ClientError::UrlError( @@ -380,7 +396,13 @@ impl Client { }; let (url, req) = self.api_request(req)?; - let body = req.send()?.text()?; + let rsp = req.send()?; + let rspstatus = rsp.status(); + if !rspstatus.is_success() { + return Err(ClientError::UrlError( + url.clone(), rspstatus.to_string())); + } + let body = rsp.text()?; // Decode the JSON response as a different kind of type // depending on the feed. But in all cases we expect to end up @@ -477,8 +499,13 @@ impl Client { }; let client = reqwest::blocking::Client::new(); - let (_url, req) = self.api_request_cl(&client, req)?; + let (url, req) = self.api_request_cl(&client, req)?; let mut rsp = req.send()?; + let rspstatus = rsp.status(); + if !rspstatus.is_success() { + return Err(ClientError::UrlError( + url.clone(), rspstatus.to_string())); + } let id = id.clone(); -- 2.30.2