Skip to content

Commit

Permalink
fix: map deserde error to ErrorResp if it is an error payload
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsse committed Feb 28, 2024
1 parent 7b35f83 commit ca4889f
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions crates/transport-http/src/reqwest.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::Http;
use alloy_json_rpc::{RequestPacket, ResponsePacket};
use alloy_json_rpc::{ErrorPayload, RequestPacket, ResponsePacket};
use alloy_transport::{TransportError, TransportErrorKind, TransportFut};
use std::task;
use tower::Service;
Expand All @@ -18,8 +18,14 @@ impl Http<reqwest::Client> {
.map_err(TransportErrorKind::custom)?;
let body = resp.bytes().await.map_err(TransportErrorKind::custom)?;

serde_json::from_slice(&body)
.map_err(|err| TransportError::deser_err(err, String::from_utf8_lossy(&body)))
serde_json::from_slice(&body).map_err(|err| {
// check if the response is an error payload
if let Ok(err) = serde_json::from_slice::<ErrorPayload>(&body) {
return TransportError::ErrorResp(err);
}

TransportError::deser_err(err, String::from_utf8_lossy(&body))
})
})
}
}
Expand Down

0 comments on commit ca4889f

Please sign in to comment.