Skip to content

Commit

Permalink
client: Add debug logging of unparsed server responses
Browse files Browse the repository at this point in the history
  • Loading branch information
lann committed Aug 28, 2023
1 parent d2d06f4 commit b81cbd0
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions crates/client/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,23 @@ async fn deserialize<T: DeserializeOwned>(response: Response) -> Result<T, Clien
let status = response.status();
match response.headers().get("content-type") {
Some(content_type) if content_type == "application/json" => {
match response.json::<T>().await {
Ok(e) => Ok(e),
Err(e) => Err(ClientError::UnexpectedResponse {
let bytes = response
.bytes()
.await
.map_err(|e| ClientError::UnexpectedResponse {
status,
message: format!("failed to read response: {e}"),
})?;
serde_json::from_slice(&bytes).map_err(|e| {
tracing::debug!(
"Unexpected response body: {}",
String::from_utf8_lossy(&bytes)
);
ClientError::UnexpectedResponse {
status,
message: format!("failed to deserialize JSON response: {e}"),
}),
}
}
})
}
Some(ty) => Err(ClientError::UnexpectedResponse {
status,
Expand Down

0 comments on commit b81cbd0

Please sign in to comment.