Skip to content

Commit

Permalink
Revert "feat: improved RPC's response error handling with HTTP status…
Browse files Browse the repository at this point in the history
… codes (#11806)"

This reverts commit db0644f.
  • Loading branch information
VanBarbascu committed Jul 31, 2024
1 parent 696a846 commit c960d5c
Showing 1 changed file with 53 additions and 43 deletions.
96 changes: 53 additions & 43 deletions chain/jsonrpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use actix_web::HttpRequest;
use actix_web::{get, http, middleware, web, App, Error as HttpError, HttpResponse, HttpServer};
use api::RpcRequest;
pub use api::{RpcFrom, RpcInto};
use futures::Future;
use futures::FutureExt;
use near_async::actix::ActixResult;
use near_async::messaging::{
AsyncSendError, AsyncSender, CanSend, MessageWithCallback, SendAsync, Sender,
Expand All @@ -19,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, RpcErrorKind};
use near_jsonrpc_primitives::errors::RpcError;
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 @@ -286,13 +288,15 @@ struct JsonRpcHandler {
}

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

Expand Down Expand Up @@ -1351,34 +1355,29 @@ impl JsonRpcHandler {
}
}

async fn rpc_handler(
fn rpc_handler(
message: web::Json<Message>,
handler: web::Data<JsonRpcHandler>,
) -> HttpResponse {
let message = handler.process(message.0).await;
let mut response = if let Message::Response(response) = &message {
match &response.result {
Ok(_) => HttpResponse::Ok(),
Err(err) => match err.error_struct {
Some(RpcErrorKind::InternalError(_)) | Some(RpcErrorKind::HandlerError(_)) => {
HttpResponse::InternalServerError()
}
_ => HttpResponse::Ok(),
},
}
} else {
HttpResponse::InternalServerError()
) -> impl Future<Output = Result<HttpResponse, HttpError>> {
let response = async move {
let message = handler.process(message.0).await?;
Ok(HttpResponse::Ok().json(&message))
};
response.json(message)
response.boxed()
}

async fn status_handler(handler: web::Data<JsonRpcHandler>) -> Result<HttpResponse, HttpError> {
fn status_handler(
handler: web::Data<JsonRpcHandler>,
) -> impl Future<Output = Result<HttpResponse, HttpError>> {
metrics::HTTP_STATUS_REQUEST_COUNT.inc();

match handler.status().await {
Ok(value) => Ok(HttpResponse::Ok().json(&value)),
Err(_) => Ok(HttpResponse::ServiceUnavailable().finish()),
}
let response = async move {
match handler.status().await {
Ok(value) => Ok(HttpResponse::Ok().json(&value)),
Err(_) => Ok(HttpResponse::ServiceUnavailable().finish()),
}
};
response.boxed()
}

async fn debug_handler(
Expand Down Expand Up @@ -1421,20 +1420,28 @@ async fn debug_block_status_handler(
}
}

async fn health_handler(handler: web::Data<JsonRpcHandler>) -> Result<HttpResponse, HttpError> {
match handler.health().await {
Ok(value) => Ok(HttpResponse::Ok().json(&value)),
Err(_) => Ok(HttpResponse::ServiceUnavailable().finish()),
}
fn health_handler(
handler: web::Data<JsonRpcHandler>,
) -> impl Future<Output = Result<HttpResponse, HttpError>> {
let response = async move {
match handler.health().await {
Ok(value) => Ok(HttpResponse::Ok().json(&value)),
Err(_) => Ok(HttpResponse::ServiceUnavailable().finish()),
}
};
response.boxed()
}

async fn network_info_handler(
fn network_info_handler(
handler: web::Data<JsonRpcHandler>,
) -> Result<HttpResponse, HttpError> {
match handler.network_info().await {
Ok(value) => Ok(HttpResponse::Ok().json(&value)),
Err(_) => Ok(HttpResponse::ServiceUnavailable().finish()),
}
) -> impl Future<Output = Result<HttpResponse, HttpError>> {
let response = async move {
match handler.network_info().await {
Ok(value) => Ok(HttpResponse::Ok().json(&value)),
Err(_) => Ok(HttpResponse::ServiceUnavailable().finish()),
}
};
response.boxed()
}

pub async fn prometheus_handler() -> Result<HttpResponse, HttpError> {
Expand All @@ -1450,13 +1457,16 @@ pub async fn prometheus_handler() -> Result<HttpResponse, HttpError> {
}
}

async fn client_config_handler(
fn client_config_handler(
handler: web::Data<JsonRpcHandler>,
) -> Result<HttpResponse, HttpError> {
match handler.client_config().await {
Ok(value) => Ok(HttpResponse::Ok().json(&value)),
Err(_) => Ok(HttpResponse::ServiceUnavailable().finish()),
}
) -> impl Future<Output = Result<HttpResponse, HttpError>> {
let response = async move {
match handler.client_config().await {
Ok(value) => Ok(HttpResponse::Ok().json(&value)),
Err(_) => Ok(HttpResponse::ServiceUnavailable().finish()),
}
};
response.boxed()
}

fn get_cors(cors_allowed_origins: &[String]) -> Cors {
Expand Down

0 comments on commit c960d5c

Please sign in to comment.