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
+14 -27
View File
@@ -29,51 +29,38 @@ pub type Result<T> = std::result::Result<T, Error>;
pub type FutureResult<T> = jsonrpc_core::BoxFuture<Result<T>>;
/// Author RPC errors.
#[derive(Debug, derive_more::Display, derive_more::From)]
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// Client error.
#[display(fmt = "Client error: {}", _0)]
#[from(ignore)]
#[error("Client error: {}", .0)]
Client(Box<dyn std::error::Error + Send>),
/// Transaction pool error,
#[display(fmt = "Transaction pool error: {}", _0)]
Pool(sc_transaction_pool_api::error::Error),
#[error("Transaction pool error: {}", .0)]
Pool(#[from] sc_transaction_pool_api::error::Error),
/// Verification error
#[display(fmt = "Extrinsic verification error: {}", _0)]
#[from(ignore)]
#[error("Extrinsic verification error: {}", .0)]
Verification(Box<dyn std::error::Error + Send>),
/// Incorrect extrinsic format.
#[display(fmt = "Invalid extrinsic format: {}", _0)]
BadFormat(codec::Error),
#[error("Invalid extrinsic format: {}", .0)]
BadFormat(#[from] codec::Error),
/// Incorrect seed phrase.
#[display(fmt = "Invalid seed phrase/SURI")]
#[error("Invalid seed phrase/SURI")]
BadSeedPhrase,
/// Key type ID has an unknown format.
#[display(fmt = "Invalid key type ID format (should be of length four)")]
#[error("Invalid key type ID format (should be of length four)")]
BadKeyType,
/// Key type ID has some unsupported crypto.
#[display(fmt = "The crypto of key type ID is unknown")]
#[error("The crypto of key type ID is unknown")]
UnsupportedKeyType,
/// Some random issue with the key store. Shouldn't happen.
#[display(fmt = "The key store is unavailable")]
#[error("The key store is unavailable")]
KeyStoreUnavailable,
/// Invalid session keys encoding.
#[display(fmt = "Session keys are not encoded correctly")]
#[error("Session keys are not encoded correctly")]
InvalidSessionKeys,
/// 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),
Error::Pool(ref err) => Some(err),
Error::Verification(ref err) => Some(&**err),
Error::UnsafeRpcCalled(ref err) => Some(err),
_ => None,
}
}
#[error(transparent)]
UnsafeRpcCalled(#[from] crate::policy::UnsafeRpcError),
}
/// Base code for all authorship errors.
+4 -12
View File
@@ -28,24 +28,16 @@ pub type Result<T> = std::result::Result<T, Error>;
pub type FutureResult<T> = jsonrpc_core::BoxFuture<Result<T>>;
/// Chain 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>),
/// Other error type.
#[error("{0}")]
Other(String),
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Client(ref err) => Some(&**err),
_ => None,
}
}
}
/// Base error code for all chain errors.
const BASE_ERROR: i64 = 3000;
+4 -12
View File
@@ -24,22 +24,14 @@ use jsonrpc_core as rpc;
pub type Result<T> = std::result::Result<T, Error>;
/// Offchain RPC errors.
#[derive(Debug, derive_more::Display, derive_more::From)]
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// Unavailable storage kind error.
#[display(fmt = "This storage kind is not available yet.")]
#[error("This storage kind is not available yet.")]
UnavailableStorageKind,
/// 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 {
Self::UnsafeRpcCalled(err) => Some(err),
_ => None,
}
}
#[error(transparent)]
UnsafeRpcCalled(#[from] crate::policy::UnsafeRpcError),
}
/// Base error code for all offchain errors.
+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.
+3 -4
View File
@@ -25,17 +25,16 @@ use jsonrpc_core as rpc;
pub type Result<T> = std::result::Result<T, Error>;
/// System RPC errors.
#[derive(Debug, derive_more::Display, derive_more::From)]
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// Provided block range couldn't be resolved to a list of blocks.
#[display(fmt = "Node is not fully functional: {}", _0)]
#[error("Node is not fully functional: {}", .0)]
NotHealthy(Health),
/// Peer argument is malformatted.
#[error("{0}")]
MalformattedPeerArg(String),
}
impl std::error::Error for Error {}
/// Base code for all system errors.
const BASE_ERROR: i64 = 2000;