chiark / gitweb /
Report HTTP errors from all URLs we retrieve.
authorSimon Tatham <anakin@pobox.com>
Sun, 31 Dec 2023 14:01:44 +0000 (14:01 +0000)
committerSimon Tatham <anakin@pobox.com>
Sun, 31 Dec 2023 14:06:32 +0000 (14:06 +0000)
src/client.rs

index da6fdd52d80ffece1d3a32705d515d2cfea3daa9..0a7cdfa90df3d5a222421cf3f02bb69d61ad5d9d 100644 (file)
@@ -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();