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

fix(servers): more descriptive errors when calls fail #790

Merged
merged 4 commits into from
Jun 13, 2022
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
18 changes: 14 additions & 4 deletions core/src/server/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
use std::io;
use std::sync::Arc;

use crate::{to_json_raw_value, Error};
use crate::Error;
use futures_channel::mpsc;
use futures_util::StreamExt;
use jsonrpsee_types::error::{ErrorCode, ErrorObject, ErrorResponse, OVERSIZED_RESPONSE_CODE, OVERSIZED_RESPONSE_MSG};
Expand Down Expand Up @@ -119,8 +119,8 @@ impl MethodSink {
tracing::error!("Error serializing response: {:?}", err);

if err.is_io() {
let data = to_json_raw_value(&format!("Exceeded max limit {}", self.max_response_size)).ok();
let err = ErrorObject::borrowed(OVERSIZED_RESPONSE_CODE, &OVERSIZED_RESPONSE_MSG, data.as_deref());
let data = format!("Exceeded max limit {}", self.max_response_size);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes more sense to use ErrrorObject::owned when we already have to allocate a String here.

let err = ErrorObject::owned(OVERSIZED_RESPONSE_CODE, OVERSIZED_RESPONSE_MSG, Some(data));
return self.send_error(id, err);
} else {
return self.send_error(id, ErrorCode::InternalError.into());
Expand Down Expand Up @@ -217,12 +217,17 @@ impl SubscriptionPermit {
pub struct BoundedSubscriptions {
resource: Arc<Notify>,
guard: Arc<Semaphore>,
max: u32,
}

impl BoundedSubscriptions {
/// Create a new bounded subscription.
pub fn new(max_subscriptions: u32) -> Self {
Self { resource: Arc::new(Notify::new()), guard: Arc::new(Semaphore::new(max_subscriptions as usize)) }
Self {
resource: Arc::new(Notify::new()),
guard: Arc::new(Semaphore::new(max_subscriptions as usize)),
max: max_subscriptions,
}
}

/// Attempts to acquire a subscription slot.
Expand All @@ -235,6 +240,11 @@ impl BoundedSubscriptions {
.map(|p| SubscriptionPermit { _permit: p, resource: self.resource.clone() })
}

/// Get the maximum number of permitted subscriptions.
pub const fn max(&self) -> u32 {
self.max
}

/// Close all subscriptions.
pub fn close(&self) {
self.resource.notify_waiters();
Expand Down
6 changes: 4 additions & 2 deletions http-server/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

//! Contains common builders for hyper responses.

use jsonrpsee_types::error::reject_too_big_request;

use crate::types::error::{ErrorCode, ErrorResponse};
use crate::types::Id;

Expand Down Expand Up @@ -73,8 +75,8 @@ pub fn invalid_allow_headers() -> hyper::Response<hyper::Body> {
}

/// Create a json response for oversized requests (413)
pub fn too_large() -> hyper::Response<hyper::Body> {
let error = serde_json::to_string(&ErrorResponse::borrowed(ErrorCode::OversizedRequest.into(), Id::Null))
pub fn too_large(limit: u32) -> hyper::Response<hyper::Body> {
let error = serde_json::to_string(&ErrorResponse::borrowed(reject_too_big_request(limit), Id::Null))
.expect("built from known-good data; qed");

from_template(hyper::StatusCode::PAYLOAD_TOO_LARGE, error, JSON)
Expand Down
2 changes: 1 addition & 1 deletion http-server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ async fn process_validated_request(

let (body, mut is_single) = match read_body(&parts.headers, body, max_request_body_size).await {
Ok(r) => r,
Err(GenericTransportError::TooLarge) => return Ok(response::too_large()),
Err(GenericTransportError::TooLarge) => return Ok(response::too_large(max_request_body_size)),
Err(GenericTransportError::Malformed) => return Ok(response::malformed()),
Err(GenericTransportError::Inner(e)) => {
tracing::error!("Internal error reading request body: {}", e);
Expand Down
22 changes: 22 additions & 0 deletions types/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ pub const SUBSCRIPTION_CLOSED: i32 = -32003;
pub const SUBSCRIPTION_CLOSED_WITH_ERROR: i32 = -32004;
/// Batched requests are not supported by the server.
pub const BATCHES_NOT_SUPPORTED_CODE: i32 = -32005;
/// Subscription limit per connection was exceeded.
pub const SUBSCRIPTIONS_LIMIT_PER_CONN: i32 = -32006;

/// Parse error message
pub const PARSE_ERROR_MSG: &str = "Parse error";
Expand All @@ -203,6 +205,8 @@ pub const SERVER_IS_BUSY_MSG: &str = "Server is busy, try again later";
pub const SERVER_ERROR_MSG: &str = "Server error";
/// Batched requests not supported error message.
pub const BATCHES_NOT_SUPPORTED_MSG: &str = "Batched requests are not supported by this server";
/// Subscription limit per connection was exceeded.
pub const SUBSCRIPTIONS_LIMIT_PER_CONN_MSG: &str = "Too many subscriptions on the connection";

/// JSONRPC error code
#[derive(Error, Debug, PartialEq, Copy, Clone)]
Expand Down Expand Up @@ -322,6 +326,24 @@ impl CallError {
}
}

/// Helper to get a `JSON-RPC` error object when the maximum number of subscriptions have been exceeded.
pub fn reject_too_many_subscriptions(limit: u32) -> ErrorObject<'static> {
ErrorObjectOwned::owned(
SUBSCRIPTIONS_LIMIT_PER_CONN,
SUBSCRIPTIONS_LIMIT_PER_CONN_MSG,
Some(format!("Exceeded max limit {}", limit)),
)
}

/// Helper to get a `JSON-RPC` error object when the maximum request size limit have been exceeded.
pub fn reject_too_big_request(limit: u32) -> ErrorObject<'static> {
ErrorObjectOwned::owned(
OVERSIZED_REQUEST_CODE,
OVERSIZED_REQUEST_MSG,
Some(format!("Exceeded max limit {}", limit)),
)
}

#[cfg(test)]
mod tests {
use super::{ErrorCode, ErrorObject, ErrorResponse, Id, TwoPointZero};
Expand Down
13 changes: 10 additions & 3 deletions ws-server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ use jsonrpsee_core::server::resource_limiting::Resources;
use jsonrpsee_core::server::rpc_module::{ConnState, ConnectionId, MethodKind, Methods};
use jsonrpsee_core::traits::IdProvider;
use jsonrpsee_core::{Error, TEN_MB_SIZE_BYTES};
use jsonrpsee_types::error::{reject_too_big_request, reject_too_many_subscriptions};
use jsonrpsee_types::Params;
use soketto::connection::Error as SokettoError;
use soketto::data::ByteSlice125;
Expand Down Expand Up @@ -397,7 +398,7 @@ async fn background_task(
current,
maximum
);
sink.send_error(Id::Null, ErrorCode::OversizedRequest.into());
sink.send_error(Id::Null, reject_too_big_request(max_request_body_size));
continue;
}
// These errors can not be gracefully handled, so just log them and terminate the connection.
Expand Down Expand Up @@ -483,7 +484,10 @@ async fn background_task(
ConnState { conn_id, close_notify: cn, id_provider: &*id_provider };
callback(id, params, &sink, conn_state)
} else {
sink.send_error(req.id, ErrorCode::ServerIsBusy.into());
sink.send_error(
req.id,
reject_too_many_subscriptions(bounded_subscriptions.max()),
);
false
};
middleware.on_result(name, result, request_start);
Expand Down Expand Up @@ -602,7 +606,10 @@ async fn background_task(
};
callback(id, params, &sink_batch, conn_state)
} else {
sink_batch.send_error(req.id, ErrorCode::ServerIsBusy.into());
sink_batch.send_error(
req.id,
reject_too_many_subscriptions(bounded_subscriptions2.max()),
);
false
};
middleware.on_result(&req.method, result, request_start);
Expand Down