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

Turn non-200 HTTP responses into an error instead of trying to parse the body as a JSON-RPC response #1361

Merged
merged 4 commits into from
Sep 29, 2023
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
3 changes: 3 additions & 0 deletions .changelog/unreleased/improvements/1359-hande-http-error.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- `[tendermint-rpc]` Turn non-200 HTTP response into an error
instead of trying to parse the body as a JSON-RPC response
([\#1359](https://github.com/informalsystems/tendermint-rs/issues/1359))
2 changes: 1 addition & 1 deletion rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ thiserror = { version = "1", default-features = false }
time = { version = "0.3", default-features = false, features = ["macros", "parsing"] }
uuid = { version = "0.8", default-features = false }
subtle-encoding = { version = "0.5", default-features = false, features = ["bech32-preview"] }
url = { version = "2.2", default-features = false }
url = { version = "2.4.1", default-features = false }
walkdir = { version = "2.3", default-features = false }
flex-error = { version = "0.4.4", default-features = false }
subtle = { version = "2", default-features = false }
Expand Down
20 changes: 16 additions & 4 deletions rpc/src/client/transport/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ impl HttpClient {
{
let request_body = request.into_json();

tracing::trace!("outgoing request: {}", request_body);
tracing::debug!(url = %self.url, body = %request_body, "outgoing request");

let mut builder = self
.inner
Expand All @@ -182,11 +182,23 @@ impl HttpClient {
{
let request = self.build_request(request)?;
let response = self.inner.execute(request).await.map_err(Error::http)?;
let response_status = response.status();
let response_body = response.bytes().await.map_err(Error::http)?;
tracing::trace!(
"incoming response: {}",
String::from_utf8_lossy(&response_body)

tracing::debug!(
status = %response_status,
body = %String::from_utf8_lossy(&response_body),
"incoming response"
);

// Successful JSON-RPC requests are expected to return a 200 OK HTTP status.
// Otherwise, this means that the HTTP request failed as a whole,
// as opposed to the JSON-RPC request returning an error,
// and we cannot expect the response body to be a valid JSON-RPC response.
if response_status != reqwest::StatusCode::OK {
return Err(Error::http_request_failed(response_status));
}

R::Response::from_string(&response_body).map(Into::into)
}
}
Expand Down
14 changes: 14 additions & 0 deletions rpc/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ type ReqwestError = flex_error::DisplayOnly<reqwest::Error>;
#[cfg(not(feature = "reqwest"))]
type ReqwestError = flex_error::NoSource;

#[cfg(feature = "reqwest")]
type HttpStatusCode = reqwest::StatusCode;

#[cfg(not(feature = "reqwest"))]
type HttpStatusCode = core::num::NonZeroU16;

#[cfg(feature = "tokio")]
type JoinError = flex_error::DisplayOnly<tokio::task::JoinError>;

Expand Down Expand Up @@ -77,6 +83,14 @@ define_error! {
format_args!("method not found: {}", e.method)
},

HttpRequestFailed
{
status: HttpStatusCode,
}
| e | {
format_args!("HTTP request failed with non-200 status code: {}", e.status)
},

Parse
{
reason: String
Expand Down