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
+1 -1
View File
@@ -8095,7 +8095,6 @@ dependencies = [
name = "sc-rpc-api"
version = "0.10.0-dev"
dependencies = [
"derive_more",
"futures 0.3.16",
"jsonrpc-core",
"jsonrpc-core-client",
@@ -8113,6 +8112,7 @@ dependencies = [
"sp-runtime",
"sp-tracing",
"sp-version",
"thiserror",
]
[[package]]
+2 -1
View File
@@ -14,7 +14,6 @@ targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
codec = { package = "parity-scale-codec", version = "2.0.0" }
derive_more = "0.99.2"
futures = "0.3.16"
jsonrpc-core = "18.0.0"
jsonrpc-core-client = "18.0.0"
@@ -22,6 +21,8 @@ jsonrpc-derive = "18.0.0"
jsonrpc-pubsub = "18.0.0"
log = "0.4.8"
parking_lot = "0.11.1"
thiserror = "1.0"
sp-core = { version = "4.0.0-dev", path = "../../primitives/core" }
sp-version = { version = "4.0.0-dev", path = "../../primitives/version" }
sp-runtime = { path = "../../primitives/runtime", version = "4.0.0-dev" }
+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;
+4 -2
View File
@@ -87,7 +87,7 @@ where
// FIXME <2329>: Database seems to limit the block number to u32 for no reason
let block_num: u32 = num_or_hex.try_into().map_err(|_| {
Error::from(format!(
Error::Other(format!(
"`{:?}` > u32::MAX, the max block number is u32.",
num_or_hex
))
@@ -332,7 +332,9 @@ fn subscribe_headers<Block, Client, F, G, S>(
let header = client
.header(BlockId::Hash(best_block_hash()))
.map_err(client_err)
.and_then(|header| header.ok_or_else(|| "Best header missing.".to_string().into()))
.and_then(|header| {
header.ok_or_else(|| Error::Other("Best header missing.".to_string()))
})
.map_err(Into::into);
// send further subscriptions