Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
use thiserror instead of derive_more for error handling (#10696)
Browse files Browse the repository at this point in the history
* use thiserror instead of derive_more for error handling

Signed-off-by: koushiro <koushiro.cqx@gmail.com>

* Update utils/prometheus/src/lib.rs

* Update utils/prometheus/src/lib.rs

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
  • Loading branch information
koushiro and bkchr authored Jan 25, 2022
1 parent 9e9e4d3 commit f2f9970
Show file tree
Hide file tree
Showing 47 changed files with 376 additions and 355 deletions.
30 changes: 13 additions & 17 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bin/node/inspect/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
clap = { version = "3.0", features = ["derive"] }
codec = { package = "parity-scale-codec", version = "2.0.0" }
derive_more = "0.99"
thiserror = "1.0"
sc-cli = { version = "0.10.0-dev", path = "../../../client/cli" }
sc-client-api = { version = "4.0.0-dev", path = "../../../client/api" }
sc-executor = { version = "0.10.0-dev", path = "../../../client/executor" }
Expand Down
19 changes: 6 additions & 13 deletions bin/node/inspect/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,26 +78,19 @@ impl<TBlock: Block> PrettyPrinter<TBlock> for DebugPrinter {
}

/// Aggregated error for `Inspector` operations.
#[derive(Debug, derive_more::From, derive_more::Display)]
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// Could not decode Block or Extrinsic.
Codec(codec::Error),
#[error(transparent)]
Codec(#[from] codec::Error),
/// Error accessing blockchain DB.
Blockchain(sp_blockchain::Error),
#[error(transparent)]
Blockchain(#[from] sp_blockchain::Error),
/// Given block has not been found.
#[error("{0}")]
NotFound(String),
}

impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match *self {
Self::Codec(ref e) => Some(e),
Self::Blockchain(ref e) => Some(e),
Self::NotFound(_) => None,
}
}
}

/// A helper trait to access block headers and bodies.
pub trait ChainAccess<TBlock: Block>: HeaderBackend<TBlock> + BlockBackend<TBlock> {}

Expand Down
2 changes: 1 addition & 1 deletion client/authority-discovery/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ prost-build = "0.9"
[dependencies]
async-trait = "0.1"
codec = { package = "parity-scale-codec", default-features = false, version = "2.0.0" }
derive_more = "0.99.16"
thiserror = "1.0"
futures = "0.3.9"
futures-timer = "3.0.1"
ip_network = "0.4.1"
Expand Down
68 changes: 42 additions & 26 deletions client/authority-discovery/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,56 @@ use sp_core::crypto::CryptoTypePublicPair;
pub type Result<T> = std::result::Result<T, Error>;

/// Error type for the authority discovery module.
#[derive(Debug, derive_more::Display, derive_more::From)]
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// Received dht value found event with records with different keys.
#[error("Received dht value found event with records with different keys.")]
ReceivingDhtValueFoundEventWithDifferentKeys,
/// Received dht value found event with no records.

#[error("Received dht value found event with no records.")]
ReceivingDhtValueFoundEventWithNoRecords,
/// Failed to verify a dht payload with the given signature.

#[error("Failed to verify a dht payload with the given signature.")]
VerifyingDhtPayload,
/// Failed to hash the authority id to be used as a dht key.
HashingAuthorityId(libp2p::core::multiaddr::multihash::Error),
/// Failed calling into the Substrate runtime.
CallingRuntime(sp_blockchain::Error),
/// Received a dht record with a key that does not match any in-flight awaited keys.

#[error("Failed to hash the authority id to be used as a dht key.")]
HashingAuthorityId(#[from] libp2p::core::multiaddr::multihash::Error),

#[error("Failed calling into the Substrate runtime.")]
CallingRuntime(#[from] sp_blockchain::Error),

#[error("Received a dht record with a key that does not match any in-flight awaited keys.")]
ReceivingUnexpectedRecord,
/// Failed to encode a protobuf payload.
EncodingProto(prost::EncodeError),
/// Failed to decode a protobuf payload.
DecodingProto(prost::DecodeError),
/// Failed to encode or decode scale payload.
EncodingDecodingScale(codec::Error),
/// Failed to parse a libp2p multi address.
ParsingMultiaddress(libp2p::core::multiaddr::Error),
/// Failed to parse a libp2p key.
ParsingLibp2pIdentity(sc_network::DecodingError),
/// Failed to sign using a specific public key.

#[error("Failed to encode a protobuf payload.")]
EncodingProto(#[from] prost::EncodeError),

#[error("Failed to decode a protobuf payload.")]
DecodingProto(#[from] prost::DecodeError),

#[error("Failed to encode or decode scale payload.")]
EncodingDecodingScale(#[from] codec::Error),

#[error("Failed to parse a libp2p multi address.")]
ParsingMultiaddress(#[from] libp2p::core::multiaddr::Error),

#[error("Failed to parse a libp2p key.")]
ParsingLibp2pIdentity(#[from] sc_network::DecodingError),

#[error("Failed to sign using a specific public key.")]
MissingSignature(CryptoTypePublicPair),
/// Failed to sign using all public keys.

#[error("Failed to sign using all public keys.")]
Signing,
/// Failed to register Prometheus metric.
Prometheus(prometheus_endpoint::PrometheusError),
/// Received authority record that contains addresses with multiple peer ids

#[error("Failed to register Prometheus metric.")]
Prometheus(#[from] prometheus_endpoint::PrometheusError),

#[error("Received authority record that contains addresses with multiple peer ids")]
ReceivingDhtValueFoundEventWithDifferentPeerIds,
/// Received authority record without any addresses having a peer id

#[error("Received authority record without any addresses having a peer id")]
ReceivingDhtValueFoundEventWithNoPeerIds,
/// Received authority record without a valid signature for the remote peer id.

#[error("Received authority record without a valid signature for the remote peer id.")]
MissingPeerIdSignature,
}
1 change: 0 additions & 1 deletion client/beefy/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ repository = "https://github.com/paritytech/substrate"
description = "RPC for the BEEFY Client gadget for substrate"

[dependencies]
derive_more = "0.99"
futures = "0.3.16"
log = "0.4"
parking_lot = "0.11"
Expand Down
8 changes: 4 additions & 4 deletions client/beefy/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ mod notification;

type FutureResult<T> = jsonrpc_core::BoxFuture<Result<T, jsonrpc_core::Error>>;

#[derive(Debug, derive_more::Display, derive_more::From, thiserror::Error)]
#[derive(Debug, thiserror::Error)]
/// Top-level error type for the RPC handler
pub enum Error {
/// The BEEFY RPC endpoint is not ready.
#[display(fmt = "BEEFY RPC endpoint not ready")]
#[error("BEEFY RPC endpoint not ready")]
EndpointNotReady,
/// The BEEFY RPC background task failed to spawn.
#[display(fmt = "BEEFY RPC background task failed to spawn")]
RpcTaskFailure(SpawnError),
#[error("BEEFY RPC background task failed to spawn")]
RpcTaskFailure(#[from] SpawnError),
}

/// The error codes returned by jsonrpc.
Expand Down
Loading

0 comments on commit f2f9970

Please sign in to comment.