use thiserror instead of derive_more for error handling (#10696)

* 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>
This commit is contained in:
Qinxuan Chen
2022-01-26 03:48:46 +08:00
committed by GitHub
parent 38d94d6323
commit e956c2e1c7
47 changed files with 378 additions and 357 deletions
+1 -1
View File
@@ -22,7 +22,7 @@ codec = { package = "parity-scale-codec", version = "2.0.0" }
sp-consensus = { version = "0.10.0-dev", path = "../../../primitives/consensus/common" }
sc-consensus = { version = "0.10.0-dev", path = "../../../client/consensus/common" }
sp-consensus-slots = { version = "0.10.0-dev", path = "../../../primitives/consensus/slots" }
derive_more = "0.99.16"
thiserror = "1.0"
futures = "0.3.9"
sp-inherents = { version = "4.0.0-dev", path = "../../../primitives/inherents" }
log = "0.4.8"
+10 -9
View File
@@ -491,33 +491,34 @@ fn aura_err<B: BlockT>(error: Error<B>) -> Error<B> {
}
/// Aura Errors
#[derive(derive_more::Display, Debug)]
#[derive(Debug, thiserror::Error)]
pub enum Error<B: BlockT> {
/// Multiple Aura pre-runtime headers
#[display(fmt = "Multiple Aura pre-runtime headers")]
#[error("Multiple Aura pre-runtime headers")]
MultipleHeaders,
/// No Aura pre-runtime digest found
#[display(fmt = "No Aura pre-runtime digest found")]
#[error("No Aura pre-runtime digest found")]
NoDigestFound,
/// Header is unsealed
#[display(fmt = "Header {:?} is unsealed", _0)]
#[error("Header {0:?} is unsealed")]
HeaderUnsealed(B::Hash),
/// Header has a bad seal
#[display(fmt = "Header {:?} has a bad seal", _0)]
#[error("Header {0:?} has a bad seal")]
HeaderBadSeal(B::Hash),
/// Slot Author not found
#[display(fmt = "Slot Author not found")]
#[error("Slot Author not found")]
SlotAuthorNotFound,
/// Bad signature
#[display(fmt = "Bad signature on {:?}", _0)]
#[error("Bad signature on {0:?}")]
BadSignature(B::Hash),
/// Client Error
#[error(transparent)]
Client(sp_blockchain::Error),
/// Unknown inherent error for identifier
#[display(fmt = "Unknown inherent error for identifier: {}", "String::from_utf8_lossy(_0)")]
#[error("Unknown inherent error for identifier: {}", String::from_utf8_lossy(.0))]
UnknownInherentError(sp_inherents::InherentIdentifier),
#[display(fmt = "Inherent error: {}", _0)]
/// Inherents Error
#[error("Inherent error: {0}")]
Inherent(sp_inherents::Error),
}
+1 -1
View File
@@ -49,7 +49,7 @@ log = "0.4.8"
schnorrkel = { version = "0.9.1", features = ["preaudit_deprecated"] }
rand = "0.7.2"
merlin = "2.0"
derive_more = "0.99.16"
thiserror = "1.0"
retain_mut = "0.1.4"
async-trait = "0.1.50"
@@ -19,12 +19,12 @@ jsonrpc-core = "18.0.0"
jsonrpc-core-client = "18.0.0"
jsonrpc-derive = "18.0.0"
sp-consensus-babe = { version = "0.10.0-dev", path = "../../../../primitives/consensus/babe" }
serde = { version = "1.0.132", features=["derive"] }
serde = { version = "1.0.132", features = ["derive"] }
sp-blockchain = { version = "4.0.0-dev", path = "../../../../primitives/blockchain" }
sp-runtime = { version = "4.1.0-dev", path = "../../../../primitives/runtime" }
sc-consensus-epochs = { version = "0.10.0-dev", path = "../../epochs" }
futures = "0.3.16"
derive_more = "0.99.16"
thiserror = "1.0"
sp-api = { version = "4.0.0-dev", path = "../../../../primitives/api" }
sp-consensus = { version = "0.10.0-dev", path = "../../../../primitives/consensus/common" }
sp-core = { version = "4.1.0-dev", path = "../../../../primitives/core" }
@@ -166,11 +166,13 @@ pub struct EpochAuthorship {
}
/// Errors encountered by the RPC
#[derive(Debug, derive_more::Display, derive_more::From)]
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// Consensus error
Consensus(ConsensusError),
#[error(transparent)]
Consensus(#[from] ConsensusError),
/// Errors that can be formatted as a String
#[error("{0}")]
StringError(String),
}
+30 -31
View File
@@ -217,95 +217,94 @@ impl Epoch {
}
/// Errors encountered by the babe authorship task.
#[derive(derive_more::Display, Debug)]
#[derive(Debug, thiserror::Error)]
pub enum Error<B: BlockT> {
/// Multiple BABE pre-runtime digests
#[display(fmt = "Multiple BABE pre-runtime digests, rejecting!")]
#[error("Multiple BABE pre-runtime digests, rejecting!")]
MultiplePreRuntimeDigests,
/// No BABE pre-runtime digest found
#[display(fmt = "No BABE pre-runtime digest found")]
#[error("No BABE pre-runtime digest found")]
NoPreRuntimeDigest,
/// Multiple BABE epoch change digests
#[display(fmt = "Multiple BABE epoch change digests, rejecting!")]
#[error("Multiple BABE epoch change digests, rejecting!")]
MultipleEpochChangeDigests,
/// Multiple BABE config change digests
#[display(fmt = "Multiple BABE config change digests, rejecting!")]
#[error("Multiple BABE config change digests, rejecting!")]
MultipleConfigChangeDigests,
/// Could not extract timestamp and slot
#[display(fmt = "Could not extract timestamp and slot: {:?}", _0)]
#[error("Could not extract timestamp and slot: {0:?}")]
Extraction(sp_consensus::Error),
/// Could not fetch epoch
#[display(fmt = "Could not fetch epoch at {:?}", _0)]
#[error("Could not fetch epoch at {0:?}")]
FetchEpoch(B::Hash),
/// Header rejected: too far in the future
#[display(fmt = "Header {:?} rejected: too far in the future", _0)]
#[error("Header {0:?} rejected: too far in the future")]
TooFarInFuture(B::Hash),
/// Parent unavailable. Cannot import
#[display(fmt = "Parent ({}) of {} unavailable. Cannot import", _0, _1)]
#[error("Parent ({0}) of {1} unavailable. Cannot import")]
ParentUnavailable(B::Hash, B::Hash),
/// Slot number must increase
#[display(fmt = "Slot number must increase: parent slot: {}, this slot: {}", _0, _1)]
#[error("Slot number must increase: parent slot: {0}, this slot: {1}")]
SlotMustIncrease(Slot, Slot),
/// Header has a bad seal
#[display(fmt = "Header {:?} has a bad seal", _0)]
#[error("Header {0:?} has a bad seal")]
HeaderBadSeal(B::Hash),
/// Header is unsealed
#[display(fmt = "Header {:?} is unsealed", _0)]
#[error("Header {0:?} is unsealed")]
HeaderUnsealed(B::Hash),
/// Slot author not found
#[display(fmt = "Slot author not found")]
#[error("Slot author not found")]
SlotAuthorNotFound,
/// Secondary slot assignments are disabled for the current epoch.
#[display(fmt = "Secondary slot assignments are disabled for the current epoch.")]
#[error("Secondary slot assignments are disabled for the current epoch.")]
SecondarySlotAssignmentsDisabled,
/// Bad signature
#[display(fmt = "Bad signature on {:?}", _0)]
#[error("Bad signature on {0:?}")]
BadSignature(B::Hash),
/// Invalid author: Expected secondary author
#[display(fmt = "Invalid author: Expected secondary author: {:?}, got: {:?}.", _0, _1)]
#[error("Invalid author: Expected secondary author: {0:?}, got: {1:?}.")]
InvalidAuthor(AuthorityId, AuthorityId),
/// No secondary author expected.
#[display(fmt = "No secondary author expected.")]
#[error("No secondary author expected.")]
NoSecondaryAuthorExpected,
/// VRF verification of block by author failed
#[display(
fmt = "VRF verification of block by author {:?} failed: threshold {} exceeded",
_0,
_1
)]
#[error("VRF verification of block by author {0:?} failed: threshold {1} exceeded")]
VRFVerificationOfBlockFailed(AuthorityId, u128),
/// VRF verification failed
#[display(fmt = "VRF verification failed: {:?}", _0)]
#[error("VRF verification failed: {0:?}")]
VRFVerificationFailed(SignatureError),
/// Could not fetch parent header
#[display(fmt = "Could not fetch parent header: {:?}", _0)]
#[error("Could not fetch parent header: {0:?}")]
FetchParentHeader(sp_blockchain::Error),
/// Expected epoch change to happen.
#[display(fmt = "Expected epoch change to happen at {:?}, s{}", _0, _1)]
#[error("Expected epoch change to happen at {0:?}, s{1}")]
ExpectedEpochChange(B::Hash, Slot),
/// Unexpected config change.
#[display(fmt = "Unexpected config change")]
#[error("Unexpected config change")]
UnexpectedConfigChange,
/// Unexpected epoch change
#[display(fmt = "Unexpected epoch change")]
#[error("Unexpected epoch change")]
UnexpectedEpochChange,
/// Parent block has no associated weight
#[display(fmt = "Parent block of {} has no associated weight", _0)]
#[error("Parent block of {0} has no associated weight")]
ParentBlockNoAssociatedWeight(B::Hash),
/// Check inherents error
#[display(fmt = "Checking inherents failed: {}", _0)]
#[error("Checking inherents failed: {0}")]
CheckInherents(sp_inherents::Error),
/// Unhandled check inherents error
#[display(fmt = "Checking inherents unhandled error: {}", "String::from_utf8_lossy(_0)")]
#[error("Checking inherents unhandled error: {}", String::from_utf8_lossy(.0))]
CheckInherentsUnhandled(sp_inherents::InherentIdentifier),
/// Create inherents error.
#[display(fmt = "Creating inherents failed: {}", _0)]
#[error("Creating inherents failed: {0}")]
CreateInherents(sp_inherents::Error),
/// Client error
#[error(transparent)]
Client(sp_blockchain::Error),
/// Runtime Api error.
#[error(transparent)]
RuntimeApi(sp_api::ApiError),
/// Fork tree error
#[error(transparent)]
ForkTree(Box<fork_tree::Error<sp_blockchain::Error>>),
}
@@ -13,7 +13,7 @@ readme = "README.md"
targets = ["x86_64-unknown-linux-gnu"]
[dependencies]
derive_more = "0.99.16"
thiserror = "1.0"
futures = "0.3.9"
jsonrpc-core = "18.0.0"
jsonrpc-core-client = "18.0.0"
@@ -38,40 +38,52 @@ mod codes {
}
/// errors encountered by background block authorship task
#[derive(Debug, derive_more::Display, derive_more::From)]
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// An error occurred while importing the block
#[display(fmt = "Block import failed: {:?}", _0)]
#[error("Block import failed: {0:?}")]
BlockImportError(ImportResult),
/// Transaction pool is empty, cannot create a block
#[display(fmt = "Transaction pool is empty, set create_empty to true,\
if you want to create empty blocks")]
#[error(
"Transaction pool is empty, set create_empty to true, if you want to create empty blocks"
)]
EmptyTransactionPool,
/// encountered during creation of Proposer.
#[display(fmt = "Consensus Error: {}", _0)]
ConsensusError(ConsensusError),
#[error("Consensus Error: {0}")]
ConsensusError(#[from] ConsensusError),
/// Failed to create Inherents data
#[display(fmt = "Inherents Error: {}", _0)]
InherentError(InherentsError),
#[error("Inherents Error: {0}")]
InherentError(#[from] InherentsError),
/// error encountered during finalization
#[display(fmt = "Finalization Error: {}", _0)]
BlockchainError(BlockchainError),
#[error("Finalization Error: {0}")]
BlockchainError(#[from] BlockchainError),
/// Supplied parent_hash doesn't exist in chain
#[display(fmt = "Supplied parent_hash: {} doesn't exist in chain", _0)]
#[from(ignore)]
#[error("Supplied parent_hash: {0} doesn't exist in chain")]
BlockNotFound(String),
/// Some string error
#[display(fmt = "{}", _0)]
#[error("{0}")]
StringError(String),
/// send error
#[display(fmt = "Consensus process is terminating")]
Canceled(oneshot::Canceled),
#[error("Consensus process is terminating")]
Canceled(#[from] oneshot::Canceled),
/// send error
#[display(fmt = "Consensus process is terminating")]
SendError(SendError),
#[error("Consensus process is terminating")]
SendError(#[from] SendError),
/// Some other error.
#[display(fmt = "Other error: {}", _0)]
Other(Box<dyn std::error::Error + Send>),
#[error("Other error: {0}")]
Other(#[from] Box<dyn std::error::Error + Send>),
}
impl From<ImportResult> for Error {
fn from(err: ImportResult) -> Self {
Error::BlockImportError(err)
}
}
impl From<String> for Error {
fn from(s: String) -> Self {
Error::StringError(s)
}
}
impl Error {
+1 -1
View File
@@ -28,6 +28,6 @@ log = "0.4.8"
futures = "0.3.16"
futures-timer = "3.0.1"
parking_lot = "0.11.2"
derive_more = "0.99.16"
thiserror = "1.0"
prometheus-endpoint = { package = "substrate-prometheus-endpoint", path = "../../../utils/prometheus", version = "0.10.0-dev"}
async-trait = "0.1.50"
+23 -18
View File
@@ -72,45 +72,50 @@ use std::{
time::Duration,
};
#[derive(derive_more::Display, Debug)]
#[derive(Debug, thiserror::Error)]
pub enum Error<B: BlockT> {
#[display(fmt = "Header uses the wrong engine {:?}", _0)]
#[error("Header uses the wrong engine {0:?}")]
WrongEngine([u8; 4]),
#[display(fmt = "Header {:?} is unsealed", _0)]
#[error("Header {0:?} is unsealed")]
HeaderUnsealed(B::Hash),
#[display(fmt = "PoW validation error: invalid seal")]
#[error("PoW validation error: invalid seal")]
InvalidSeal,
#[display(fmt = "PoW validation error: preliminary verification failed")]
#[error("PoW validation error: preliminary verification failed")]
FailedPreliminaryVerify,
#[display(fmt = "Rejecting block too far in future")]
#[error("Rejecting block too far in future")]
TooFarInFuture,
#[display(fmt = "Fetching best header failed using select chain: {:?}", _0)]
#[error("Fetching best header failed using select chain: {0:?}")]
BestHeaderSelectChain(ConsensusError),
#[display(fmt = "Fetching best header failed: {:?}", _0)]
#[error("Fetching best header failed: {0:?}")]
BestHeader(sp_blockchain::Error),
#[display(fmt = "Best header does not exist")]
#[error("Best header does not exist")]
NoBestHeader,
#[display(fmt = "Block proposing error: {:?}", _0)]
#[error("Block proposing error: {0:?}")]
BlockProposingError(String),
#[display(fmt = "Fetch best hash failed via select chain: {:?}", _0)]
#[error("Fetch best hash failed via select chain: {0:?}")]
BestHashSelectChain(ConsensusError),
#[display(fmt = "Error with block built on {:?}: {:?}", _0, _1)]
#[error("Error with block built on {0:?}: {1:?}")]
BlockBuiltError(B::Hash, ConsensusError),
#[display(fmt = "Creating inherents failed: {}", _0)]
#[error("Creating inherents failed: {0}")]
CreateInherents(sp_inherents::Error),
#[display(fmt = "Checking inherents failed: {}", _0)]
#[error("Checking inherents failed: {0}")]
CheckInherents(sp_inherents::Error),
#[display(
fmt = "Checking inherents unknown error for identifier: {:?}",
"String::from_utf8_lossy(_0)"
#[error(
"Checking inherents unknown error for identifier: {:?}",
String::from_utf8_lossy(.0)
)]
CheckInherentsUnknownError(sp_inherents::InherentIdentifier),
#[display(fmt = "Multiple pre-runtime digests")]
#[error("Multiple pre-runtime digests")]
MultiplePreRuntimeDigests,
#[error(transparent)]
Client(sp_blockchain::Error),
#[error(transparent)]
Codec(codec::Error),
#[error("{0}")]
Environment(String),
#[error("{0}")]
Runtime(RuntimeString),
#[error("{0}")]
Other(String),
}