Skip to content

send original error in ProxyGetRequest #1516

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

Merged
merged 12 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 3 additions & 3 deletions server/src/middleware/http/proxy_get_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use hyper::header::{ACCEPT, CONTENT_TYPE};
use hyper::http::HeaderValue;
use hyper::{Method, Uri};
use jsonrpsee_core::BoxError;
use jsonrpsee_types::{ErrorObject, Id, RequestSer};
use jsonrpsee_types::{ErrorCode, ErrorObject, Id, RequestSer};
use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;
Expand Down Expand Up @@ -197,8 +197,8 @@ where
serde_json::from_slice::<ErrorResponse>(&bytes)
.and_then(|payload| serde_json::from_str::<ErrorObject>(&payload.error.to_string()))
.map_or_else(
|_| http::response::internal_error(),
|error| http::response::error_response(error.to_owned()),
|_| http::response::error_response(ErrorCode::InternalError.to_string()),
|error| http::response::error_response(error.to_string()),
)
};

Expand Down
14 changes: 5 additions & 9 deletions server/src/transport/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

/// Returns true if the `content_type` header indicates a valid JSON message.
pub fn is_json(content_type: Option<&hyper::header::HeaderValue>) -> bool {
content_type.and_then(|val| val.to_str().ok()).map_or(false, |content| {

Check warning on line 21 in server/src/transport/http.rs

View workflow job for this annotation

GitHub Actions / Check style

this `map_or` is redundant

Check warning on line 21 in server/src/transport/http.rs

View workflow job for this annotation

GitHub Actions / Check style

this `map_or` is redundant

Check warning on line 21 in server/src/transport/http.rs

View workflow job for this annotation

GitHub Actions / Check style

this `map_or` is redundant
content.eq_ignore_ascii_case("application/json")
|| content.eq_ignore_ascii_case("application/json; charset=utf-8")
|| content.eq_ignore_ascii_case("application/json;charset=utf-8")
Expand Down Expand Up @@ -127,15 +127,6 @@
from_template(hyper::StatusCode::INTERNAL_SERVER_ERROR, error, JSON)
}

/// Create a json response for general errors returned by the called method.
pub fn error_response(error: ErrorObjectOwned) -> HttpResponse {
let err = ResponsePayload::<()>::error(error);
let rp = Response::new(err, Id::Null);
let error = serde_json::to_string(&rp).expect("JSON serialization infallible; qed");

from_template(hyper::StatusCode::INTERNAL_SERVER_ERROR, error, JSON)
}

/// Create a text/plain response for not allowed hosts.
pub fn host_not_allowed() -> HttpResponse {
from_template(hyper::StatusCode::FORBIDDEN, "Provided Host header is not whitelisted.\n", TEXT)
Expand Down Expand Up @@ -183,6 +174,11 @@
from_template(hyper::StatusCode::OK, body, JSON)
}

/// Create a general internal error response.
pub fn error_response(body: impl Into<HttpBody>) -> HttpResponse {
from_template(hyper::StatusCode::INTERNAL_SERVER_ERROR, body, JSON)
}

/// Create a response for unsupported content type.
pub fn unsupported_content_type() -> HttpResponse {
from_template(
Expand Down
7 changes: 6 additions & 1 deletion types/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ pub type ErrorObjectOwned = ErrorObject<'static>;
/// [Failed JSON-RPC response object](https://www.jsonrpc.org/specification#response_object).
#[derive(Debug, Deserialize, Serialize, Clone, thiserror::Error)]
#[serde(deny_unknown_fields)]
#[error("{self:?}")]
pub struct ErrorObject<'a> {
/// Code
code: ErrorCode,
Expand Down Expand Up @@ -96,6 +95,12 @@ impl<'a> ErrorObject<'a> {
}
}

impl fmt::Display for ErrorObject<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}: {}", self.code(), self.message())
}
}

impl PartialEq for ErrorObject<'_> {
fn eq(&self, other: &Self) -> bool {
let this_raw = self.data.as_ref().map(|r| r.get());
Expand Down
Loading