Skip to content

Commit

Permalink
feat: improve gateway response error logging
Browse files Browse the repository at this point in the history
Enhance error handling when parsing gateway responses fails by:
- Adding trace logging of JSON structure when type parsing fails
- Adding error logging with raw response body when JSON parsing fails
  • Loading branch information
roderickvd committed Nov 25, 2024
1 parent f9ab6d0 commit 048b503
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/).
- [chore] Add Debian package metadata

### Changed
- [gateway] Improve error logging for response parsing failures
- [protocol] Parse JSON as 64-bit and truncate internally

### Deprecated
Expand Down
19 changes: 17 additions & 2 deletions src/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,23 @@ impl Gateway {
}

let response = self.http_client.execute(request).await?;
let result = response.json::<gateway::Response<T>>().await?;
trace!("{}: {result:#?}", T::METHOD);
let body = response.text().await?;

let result: gateway::Response<T> = match serde_json::from_str(&body) {
Ok(result) => {
trace!("{}: {result:#?}", T::METHOD);
result
}
Err(e) => {
if let Ok(json) = serde_json::from_str::<serde_json::Value>(&body) {
trace!("{}: {json:#?}", T::METHOD);
} else {
error!("{}: failed parsing response ({e:?})", T::METHOD);
trace!("{body}");
}
return Err(e.into());
}
};

Ok(result)
}
Expand Down

0 comments on commit 048b503

Please sign in to comment.