diff --git a/substrate/Cargo.lock b/substrate/Cargo.lock index e883ba8c60..a613b7ba03 100644 --- a/substrate/Cargo.lock +++ b/substrate/Cargo.lock @@ -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]] diff --git a/substrate/client/rpc-api/Cargo.toml b/substrate/client/rpc-api/Cargo.toml index b0d7c28b78..86fd24c24e 100644 --- a/substrate/client/rpc-api/Cargo.toml +++ b/substrate/client/rpc-api/Cargo.toml @@ -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" } diff --git a/substrate/client/rpc-api/src/author/error.rs b/substrate/client/rpc-api/src/author/error.rs index 249c8df395..c7e3ccffab 100644 --- a/substrate/client/rpc-api/src/author/error.rs +++ b/substrate/client/rpc-api/src/author/error.rs @@ -29,51 +29,38 @@ pub type Result = std::result::Result; pub type FutureResult = jsonrpc_core::BoxFuture>; /// 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), /// 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), /// 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. diff --git a/substrate/client/rpc-api/src/chain/error.rs b/substrate/client/rpc-api/src/chain/error.rs index b1ce800d27..c7f14b2dfc 100644 --- a/substrate/client/rpc-api/src/chain/error.rs +++ b/substrate/client/rpc-api/src/chain/error.rs @@ -28,24 +28,16 @@ pub type Result = std::result::Result; pub type FutureResult = jsonrpc_core::BoxFuture>; /// 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), + #[error("Client error: {}", .0)] + Client(#[from] Box), /// 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; diff --git a/substrate/client/rpc-api/src/offchain/error.rs b/substrate/client/rpc-api/src/offchain/error.rs index f2567707bc..6b8e2bfe18 100644 --- a/substrate/client/rpc-api/src/offchain/error.rs +++ b/substrate/client/rpc-api/src/offchain/error.rs @@ -24,22 +24,14 @@ use jsonrpc_core as rpc; pub type Result = std::result::Result; /// 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. diff --git a/substrate/client/rpc-api/src/state/error.rs b/substrate/client/rpc-api/src/state/error.rs index e30757f0dd..d700863476 100644 --- a/substrate/client/rpc-api/src/state/error.rs +++ b/substrate/client/rpc-api/src/state/error.rs @@ -28,13 +28,13 @@ pub type Result = std::result::Result; pub type FutureResult = jsonrpc_core::BoxFuture>; /// 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), + #[error("Client error: {}", .0)] + Client(#[from] Box), /// 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. diff --git a/substrate/client/rpc-api/src/system/error.rs b/substrate/client/rpc-api/src/system/error.rs index b16a7abb6e..4ba5125d82 100644 --- a/substrate/client/rpc-api/src/system/error.rs +++ b/substrate/client/rpc-api/src/system/error.rs @@ -25,17 +25,16 @@ use jsonrpc_core as rpc; pub type Result = std::result::Result; /// 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; diff --git a/substrate/client/rpc/src/chain/mod.rs b/substrate/client/rpc/src/chain/mod.rs index 8685b3f93c..a06c3a094b 100644 --- a/substrate/client/rpc/src/chain/mod.rs +++ b/substrate/client/rpc/src/chain/mod.rs @@ -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( 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