From 46b4cb4da25c60125e1086dfcfd335c1538debed Mon Sep 17 00:00:00 2001 From: James Date: Thu, 7 Mar 2024 15:20:26 -0800 Subject: [PATCH 1/5] fix: use error_for_status in Reqwest transport --- crates/transport-http/src/reqwest.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/crates/transport-http/src/reqwest.rs b/crates/transport-http/src/reqwest.rs index bb8777d90dd..aef8d89faf9 100644 --- a/crates/transport-http/src/reqwest.rs +++ b/crates/transport-http/src/reqwest.rs @@ -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; @@ -15,7 +16,9 @@ impl Http { .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) From 5b0a587823494953d5a6e94aad71d3b9042fb38c Mon Sep 17 00:00:00 2001 From: James Date: Thu, 7 Mar 2024 15:35:39 -0800 Subject: [PATCH 2/5] fix: capture non-200 responses in hyper --- crates/transport-http/src/hyper.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/crates/transport-http/src/hyper.rs b/crates/transport-http/src/hyper.rs index 6b51c94be9f..9a26546f222 100644 --- a/crates/transport-http/src/hyper.rs +++ b/crates/transport-http/src/hyper.rs @@ -30,11 +30,21 @@ where let resp = this.client.request(req).await.map_err(TransportErrorKind::custom)?; - // unpack json from the response body + // unpack json 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 resp.status() != hyper::StatusCode::OK { + return Err(TransportErrorKind::custom_str(&format!( + "HTTP error: {} with body: {:?}", + resp.status(), + body + ))); + } + // Deser a Box 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. From d862a68a21454a983c7a48bf66b9a27786d21477 Mon Sep 17 00:00:00 2001 From: James Date: Thu, 7 Mar 2024 15:37:41 -0800 Subject: [PATCH 3/5] nit: comment fix --- crates/transport-http/src/hyper.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/transport-http/src/hyper.rs b/crates/transport-http/src/hyper.rs index 9a26546f222..83e6ecf8489 100644 --- a/crates/transport-http/src/hyper.rs +++ b/crates/transport-http/src/hyper.rs @@ -30,7 +30,7 @@ where let resp = this.client.request(req).await.map_err(TransportErrorKind::custom)?; - // unpack json from the response body. We do this regardless of + // 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()) From 6565585f3233e45f4759fd5a77b15f5e5066cd00 Mon Sep 17 00:00:00 2001 From: James Date: Thu, 7 Mar 2024 15:40:24 -0800 Subject: [PATCH 4/5] fix: query status before moving resp --- crates/transport-http/src/hyper.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/crates/transport-http/src/hyper.rs b/crates/transport-http/src/hyper.rs index 83e6ecf8489..3dd77fec2d4 100644 --- a/crates/transport-http/src/hyper.rs +++ b/crates/transport-http/src/hyper.rs @@ -30,6 +30,8 @@ where let resp = this.client.request(req).await.map_err(TransportErrorKind::custom)?; + 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. @@ -37,11 +39,10 @@ where .await .map_err(TransportErrorKind::custom)?; - if resp.status() != hyper::StatusCode::OK { + if status != hyper::StatusCode::OK { return Err(TransportErrorKind::custom_str(&format!( "HTTP error: {} with body: {:?}", - resp.status(), - body + status, body ))); } From e10035382d50837ba6b7ac33bc10053bb572a33a Mon Sep 17 00:00:00 2001 From: James Date: Thu, 7 Mar 2024 16:19:58 -0800 Subject: [PATCH 5/5] fix: use utf8 lossy in error --- crates/transport-http/src/hyper.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/transport-http/src/hyper.rs b/crates/transport-http/src/hyper.rs index 3dd77fec2d4..c8ae8686831 100644 --- a/crates/transport-http/src/hyper.rs +++ b/crates/transport-http/src/hyper.rs @@ -41,8 +41,9 @@ where if status != hyper::StatusCode::OK { return Err(TransportErrorKind::custom_str(&format!( - "HTTP error: {} with body: {:?}", - status, body + r#"HTTP error: {} with body: "{}""#, + status, + String::from_utf8_lossy(body.as_ref()) ))); }