Skip to content

Commit

Permalink
Handle Empty Response Body
Browse files Browse the repository at this point in the history
  • Loading branch information
0xIchigo committed May 21, 2024
1 parent b9e3374 commit 6de8d6c
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/request_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,16 @@ impl RequestHandler {
///
/// # Errors
/// Returns an error if deserialization fails or if the response status indicates a failure (e.g., 404 Not Found)
async fn handle_response<T: for<'de> Deserialize<'de>>(&self, response: Response) -> Result<T> {
async fn handle_response<T: for<'de> Deserialize<'de> + Default>(&self, response: Response) -> Result<T> {
let status: StatusCode = response.status();
let path: String = response.url().path().to_string();
let body_text: String = response.text().await.unwrap_or_default();

if status.is_success() {
if body_text.is_empty() {
return Ok(T::default());
}

match serde_json::from_str::<T>(&body_text) {
Ok(data) => Ok(data),
Err(e) => {
Expand Down

0 comments on commit 6de8d6c

Please sign in to comment.