Skip to content

Commit

Permalink
return the inner json object, not a formatted string
Browse files Browse the repository at this point in the history
  • Loading branch information
dcfreire committed Jan 10, 2025
1 parent 8198ae5 commit bb63107
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 17 deletions.
8 changes: 3 additions & 5 deletions server/src/middleware/http/proxy_get_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,10 @@ where
let response = if let Ok(payload) = serde_json::from_slice::<SuccessResponse>(&bytes) {
http::response::ok_response(payload.result.to_string())
} else {
serde_json::from_slice::<ErrorResponse>(&bytes)
let error = serde_json::from_slice::<ErrorResponse>(&bytes)
.and_then(|payload| serde_json::from_str::<ErrorObject>(&payload.error.to_string()))
.map_or_else(
|_| http::response::error_response(ErrorCode::InternalError.to_string()),
|error| http::response::error_response(error.to_string()),
)
.unwrap_or_else(|_| ErrorObject::from(ErrorCode::InternalError));
http::response::error_response(error)
};

Ok(response)
Expand Down
13 changes: 7 additions & 6 deletions server/src/transport/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ where
/// 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 @@ pub mod response {
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 Expand Up @@ -174,11 +180,6 @@ pub mod response {
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: 1 addition & 6 deletions types/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ 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 @@ -95,12 +96,6 @@ 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

0 comments on commit bb63107

Please sign in to comment.