Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert non-200 http responses into errors #254

Merged
merged 5 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion crates/transport-http/src/hyper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,23 @@ where

let resp = this.client.request(req).await.map_err(TransportErrorKind::custom)?;

// unpack json from the response body
let status = resp.status();

// unpack data from the response body. We do this regardless of
// the status code, as we want to return the error in the body if
// there is one.
let body = hyper::body::to_bytes(resp.into_body())
.await
.map_err(TransportErrorKind::custom)?;

if status != hyper::StatusCode::OK {
return Err(TransportErrorKind::custom_str(&format!(
r#"HTTP error: {} with body: "{}""#,
status,
String::from_utf8_lossy(body.as_ref())
)));
}

// Deser a Box<RawValue> from the body. If deser fails, return the
// body as a string in the error. If the body is not UTF8, this will
// fail and give the empty string in the error.
Expand Down
3 changes: 3 additions & 0 deletions crates/transport-http/src/reqwest.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::Http;
use alloy_json_rpc::{RequestPacket, ResponsePacket};
use alloy_transport::{TransportError, TransportErrorKind, TransportFut};
use reqwest::Response;
use std::task;
use tower::Service;

Expand All @@ -15,7 +16,9 @@ impl Http<reqwest::Client> {
.json(&req)
.send()
.await
.and_then(Response::error_for_status)
.map_err(TransportErrorKind::custom)?;

let body = resp.bytes().await.map_err(TransportErrorKind::custom)?;

serde_json::from_slice(&body)
Expand Down
Loading