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

feat: improved RPC's response error handling with HTTP status codes #11806

Merged
merged 6 commits into from
Jul 20, 2024
Merged
Changes from 2 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
33 changes: 23 additions & 10 deletions chain/jsonrpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use near_client::{
};
use near_client_primitives::types::GetSplitStorageInfo;
pub use near_jsonrpc_client as client;
use near_jsonrpc_primitives::errors::RpcError;
use near_jsonrpc_primitives::errors::{RpcError, RpcErrorKind};
use near_jsonrpc_primitives::message::{Message, Request};
use near_jsonrpc_primitives::types::config::{RpcProtocolConfigError, RpcProtocolConfigResponse};
use near_jsonrpc_primitives::types::entity_debug::{EntityDebugHandler, EntityQuery};
Expand Down Expand Up @@ -288,15 +288,17 @@ struct JsonRpcHandler {
}

impl JsonRpcHandler {
pub async fn process(&self, message: Message) -> Result<Message, HttpError> {
pub async fn process(&self, message: Message) -> Message {
let id = message.id();
match message {
Message::Request(request) => {
Ok(Message::response(id, self.process_request(request).await))
}
_ => Ok(Message::error(RpcError::parse_error(
Message::Request(request) => self
frolvanya marked this conversation as resolved.
Show resolved Hide resolved
.process_request(request)
.await
.map(|response| Message::response(id, Ok(response)))
.unwrap_or_else(Message::error),
_ => Message::error(RpcError::parse_error(
"JSON RPC Request format was expected".to_owned(),
))),
)),
}
}

Expand Down Expand Up @@ -1358,10 +1360,21 @@ impl JsonRpcHandler {
fn rpc_handler(
message: web::Json<Message>,
handler: web::Data<JsonRpcHandler>,
) -> impl Future<Output = Result<HttpResponse, HttpError>> {
) -> impl Future<Output = HttpResponse> {
frolvanya marked this conversation as resolved.
Show resolved Hide resolved
let response = async move {
let message = handler.process(message.0).await?;
Ok(HttpResponse::Ok().json(&message))
let message = handler.process(message.0).await;
match message.clone() {
Message::Response(response) => match response.result {
Ok(_) => HttpResponse::Ok().json(message),
Err(err) => match err.error_struct {
Some(RpcErrorKind::InternalError(_)) | Some(RpcErrorKind::HandlerError(_)) => {
HttpResponse::InternalServerError().json(&Message::error(err))
}
_ => HttpResponse::Ok().json(&Message::error(err)),
},
},
_ => HttpResponse::InternalServerError().finish(),
}
frolvanya marked this conversation as resolved.
Show resolved Hide resolved
};
response.boxed()
}
Expand Down
Loading