Skip to content

Commit

Permalink
Better handle_response in request_handler.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
0xIchigo committed Apr 22, 2024
1 parent a91f314 commit d3ea89c
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/request_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ impl RequestHandler {
async fn handle_response<T: for<'de> Deserialize<'de>>(&self, path: String, response: Response) -> Result<T> {
let status: StatusCode = response.status();

if status == StatusCode::OK || status == StatusCode::CREATED {
response.json::<T>().await.map_err(HeliusError::SerdeJson)
} else {
let error_text: String = response.text().await.unwrap_or_default();
Err(HeliusError::from_response_status(status, path, error_text))
match status {
StatusCode::OK | StatusCode::CREATED => response.json::<T>().await.map_err(HeliusError::SerdeJson),
_ => {
let error_text = response.text().await.unwrap_or_else(|_| "Failed to read response body".to_string());
Err(HeliusError::from_response_status(status, path, error_text))
}
}
}
}

0 comments on commit d3ea89c

Please sign in to comment.