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

send original error in ProxyGetRequest #1516

Merged
merged 12 commits into from
Jan 14, 2025
17 changes: 13 additions & 4 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::{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 @@ -180,15 +180,24 @@ where
}

#[derive(serde::Deserialize)]
struct RpcPayload<'a> {
struct SuccessResponse<'a> {
#[serde(borrow)]
result: &'a serde_json::value::RawValue,
}

let response = if let Ok(payload) = serde_json::from_slice::<RpcPayload>(&bytes) {
#[derive(serde::Deserialize)]
struct ErrorResponse<'a> {
#[serde(borrow)]
error: ErrorObject<'a>,
}

let response = if let Ok(payload) = serde_json::from_slice::<SuccessResponse>(&bytes) {
http::response::ok_response(payload.result.to_string())
} else {
http::response::internal_error()
let error = serde_json::from_slice::<ErrorResponse>(&bytes)
.map(|payload| payload.error)
.unwrap_or_else(|_| ErrorObject::from(ErrorCode::InternalError));
http::response::error_response(error)
};

Ok(response)
Expand Down
8 changes: 7 additions & 1 deletion 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
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 @@ -111,7 +111,7 @@
/// HTTP response helpers.
pub mod response {
use jsonrpsee_types::error::{reject_too_big_request, ErrorCode};
use jsonrpsee_types::{ErrorObjectOwned, Id, Response, ResponsePayload};
use jsonrpsee_types::{ErrorObject, ErrorObjectOwned, Id, Response, ResponsePayload};

use crate::{HttpBody, HttpResponse};

Expand All @@ -127,6 +127,12 @@
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: ErrorObject) -> HttpResponse {
let error = serde_json::to_string(&error).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
Loading