rpc-api: use thiserror instead of derive_more for error handling (#9631)

Signed-off-by: koushiro <koushiro.cqx@gmail.com>
This commit is contained in:
Qinxuan Chen
2021-08-27 13:50:36 +08:00
committed by GitHub
parent 328cfbe633
commit a636b7a512
8 changed files with 39 additions and 74 deletions
+7 -15
View File
@@ -28,13 +28,13 @@ pub type Result<T> = std::result::Result<T, Error>;
pub type FutureResult<T> = jsonrpc_core::BoxFuture<Result<T>>;
/// State RPC errors.
#[derive(Debug, derive_more::Display, derive_more::From)]
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// Client error.
#[display(fmt = "Client error: {}", _0)]
Client(Box<dyn std::error::Error + Send>),
#[error("Client error: {}", .0)]
Client(#[from] Box<dyn std::error::Error + Send>),
/// Provided block range couldn't be resolved to a list of blocks.
#[display(fmt = "Cannot resolve a block range ['{:?}' ... '{:?}]. {}", from, to, details)]
#[error("Cannot resolve a block range ['{:?}' ... '{:?}]. {}", .from, .to, .details)]
InvalidBlockRange {
/// Beginning of the block range.
from: String,
@@ -44,7 +44,7 @@ pub enum Error {
details: String,
},
/// Provided count exceeds maximum value.
#[display(fmt = "count exceeds maximum value. value: {}, max: {}", value, max)]
#[error("count exceeds maximum value. value: {}, max: {}", .value, .max)]
InvalidCount {
/// Provided value
value: u32,
@@ -52,16 +52,8 @@ pub enum Error {
max: u32,
},
/// Call to an unsafe RPC was denied.
UnsafeRpcCalled(crate::policy::UnsafeRpcError),
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Client(ref err) => Some(&**err),
_ => None,
}
}
#[error(transparent)]
UnsafeRpcCalled(#[from] crate::policy::UnsafeRpcError),
}
/// Base code for all state errors.