diff --git a/Cargo.toml b/Cargo.toml index ba06c5f..3614cec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ http = "0.2" reqwest = { version = "0.11", features = ["json"] } restest_macros = "0.1.0" serde = "1.0" +serde_json = "1.0" anyhow = "1.0.58" [dev-dependencies] diff --git a/src/request.rs b/src/request.rs index 88c9e62..9e5b8f0 100644 --- a/src/request.rs +++ b/src/request.rs @@ -299,33 +299,41 @@ impl RequestResult { /// Checks if the response status meets an expected status code and convert /// the body to a concrete type. /// - /// This method uses `serde` internally, so the output type must implement + /// This method uses `serde_json`, so the output type must implement /// [`DeserializeOwned`]. /// /// # Error /// /// This method return an error if the server response status is not equal to - /// `status` or if the body can not be deserialized to the specified type. + /// `status` or if the body can not be deserialized to the specified type or if the body is incorrectly formatted. #[track_caller] pub async fn ensure_status(self, status: StatusCode) -> Result where T: DeserializeOwned, { - if self.response.status() != status { - return Err(format!("Unexpected server response code for request '{}'. Body is {}", - self.context_description, - self.response.text().await.map_err( - |err| { - format!("Unexpected server response code for request {} : {}. Unable to read response body",self.context_description, err) - } - )?)); - } + let response_status = self.response.status(); + let response_text = self.response.text().await; - self.response.json().await.map_err(|err| { - format!( - "Failed to deserialize body for request '{}': {}", + match response_text { + Err(err) => Err(format!( + "Incorrectly formatted body for request '{}': {}", self.context_description, err - ) - }) + )), + Ok(text) => { + if response_status != status { + Err(format!( + "Unexpected server response code for request '{}'. Body is {}", + self.context_description, text + )) + } else { + serde_json::from_str(&text).map_err(|err| { + format!( + "Failed to deserialize body for request '{}': {}. Body is {}", + self.context_description, err, text + ) + }) + } + } + } } }