Removed manual implementations of core::error::Error (#13370)

Co-authored-by: router <router@router.ian>
This commit is contained in:
Pelle
2024-12-18 00:01:48 +00:00
committed by GitHub
parent c51a188c72
commit ef033abaf9
13 changed files with 117 additions and 195 deletions

View File

@ -23,6 +23,7 @@ alloy-rlp.workspace = true
# misc
derive_more.workspace = true
thiserror.workspace = true
[features]
default = ["std"]
@ -31,5 +32,6 @@ std = [
"alloy-primitives/std",
"alloy-rlp/std",
"derive_more/std",
"reth-primitives-traits/std"
"reth-primitives-traits/std",
"thiserror/std"
]

View File

@ -5,60 +5,51 @@ use alloc::{
vec::Vec,
};
use core::{
fmt,
fmt::{Debug, Display},
str::FromStr,
};
/// Database error type.
#[derive(Clone, Debug, PartialEq, Eq, derive_more::Display)]
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
pub enum DatabaseError {
/// Failed to open the database.
#[display("failed to open the database: {_0}")]
#[error("failed to open the database: {_0}")]
Open(DatabaseErrorInfo),
/// Failed to create a table in the database.
#[display("failed to create a table: {_0}")]
#[error("failed to create a table: {_0}")]
CreateTable(DatabaseErrorInfo),
/// Failed to write a value into a table.
#[error(transparent)]
Write(Box<DatabaseWriteError>),
/// Failed to read a value from a table.
#[display("failed to read a value from a database table: {_0}")]
#[error("failed to read a value from a database table: {_0}")]
Read(DatabaseErrorInfo),
/// Failed to delete a `(key, value)` pair from a table.
#[display("database delete error code: {_0}")]
#[error("database delete error code: {_0}")]
Delete(DatabaseErrorInfo),
/// Failed to commit transaction changes into the database.
#[display("failed to commit transaction changes: {_0}")]
#[error("failed to commit transaction changes: {_0}")]
Commit(DatabaseErrorInfo),
/// Failed to initiate a transaction.
#[display("failed to initialize a transaction: {_0}")]
#[error("failed to initialize a transaction: {_0}")]
InitTx(DatabaseErrorInfo),
/// Failed to initialize a cursor.
#[display("failed to initialize a cursor: {_0}")]
#[error("failed to initialize a cursor: {_0}")]
InitCursor(DatabaseErrorInfo),
/// Failed to decode a key from a table.
#[display("failed to decode a key from a table")]
#[error("failed to decode a key from a table")]
Decode,
/// Failed to get database stats.
#[display("failed to get stats: {_0}")]
#[error("failed to get stats: {_0}")]
Stats(DatabaseErrorInfo),
/// Failed to use the specified log level, as it's not available.
#[display("log level {_0:?} is not available")]
#[error("log level {_0:?} is not available")]
LogLevelUnavailable(LogLevel),
/// Other unspecified error.
#[display("{_0}")]
#[error("{_0}")]
Other(String),
}
impl core::error::Error for DatabaseError {
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
match self {
Self::Write(err) => core::error::Error::source(err),
_ => Option::None,
}
}
}
/// Common error struct to propagate implementation-specific error information.
#[derive(Debug, Clone, PartialEq, Eq, derive_more::Display)]
#[display("{message} ({code})")]
@ -87,7 +78,12 @@ impl From<DatabaseWriteError> for DatabaseError {
}
/// Database write error.
#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
#[error("write operation {:?} failed for key \"{}\" in table {}: {}",
self.operation,
alloy_primitives::hex::encode(&self.key),
self.table_name,
self.info)]
pub struct DatabaseWriteError {
/// The error code and message.
pub info: DatabaseErrorInfo,
@ -99,21 +95,6 @@ pub struct DatabaseWriteError {
pub key: Vec<u8>,
}
impl fmt::Display for DatabaseWriteError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"write operation {:?} failed for key \"{}\" in table {}: {}",
self.operation,
alloy_primitives::hex::encode(&self.key),
self.table_name,
self.info
)
}
}
impl core::error::Error for DatabaseWriteError {}
/// Database write operation type.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DatabaseWriteOperation {

View File

@ -2,18 +2,16 @@ use alloc::string::{String, ToString};
use reth_fs_util::FsPathError;
/// Storage lock error.
#[derive(Debug, Clone, PartialEq, Eq, derive_more::Display)]
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
pub enum StorageLockError {
/// Write lock taken
#[display("storage directory is currently in use as read-write by another process: PID {_0}")]
#[error("storage directory is currently in use as read-write by another process: PID {_0}")]
Taken(usize),
/// Indicates other unspecified errors.
#[display("{_0}")]
#[error("{_0}")]
Other(String),
}
impl core::error::Error for StorageLockError {}
/// TODO: turn into variant once `ProviderError`
impl From<FsPathError> for StorageLockError {
fn from(error: FsPathError) -> Self {

View File

@ -10,35 +10,35 @@ use reth_static_file_types::StaticFileSegment;
pub type ProviderResult<Ok> = Result<Ok, ProviderError>;
/// Bundled errors variants thrown by various providers.
#[derive(Clone, Debug, Display, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
pub enum ProviderError {
/// Database error.
Database(DatabaseError),
#[error(transparent)]
Database(#[from] DatabaseError),
/// RLP error.
#[error("{_0}")]
Rlp(alloy_rlp::Error),
/// Filesystem path error.
#[display("{_0}")]
#[error("{_0}")]
FsPathError(String),
/// Nippy jar error.
#[display("nippy jar error: {_0}")]
#[error("nippy jar error: {_0}")]
NippyJar(String),
/// Trie witness error.
#[display("trie witness error: {_0}")]
#[error("trie witness error: {_0}")]
TrieWitnessError(String),
/// Error when recovering the sender for a transaction
#[display("failed to recover sender for transaction")]
#[error("failed to recover sender for transaction")]
SenderRecoveryError,
/// The header number was not found for the given block hash.
#[display("block hash {_0} does not exist in Headers table")]
#[error("block hash {_0} does not exist in Headers table")]
BlockHashNotFound(BlockHash),
/// A block body is missing.
#[display("block meta not found for block #{_0}")]
#[error("block meta not found for block #{_0}")]
BlockBodyIndicesNotFound(BlockNumber),
/// The transition ID was found for the given address and storage key, but the changeset was
/// not found.
#[display(
"storage change set for address {address} and key {storage_key} at block #{block_number} does not exist"
)]
#[error("storage change set for address {address} and key {storage_key} at block #{block_number} does not exist")]
StorageChangesetNotFound {
/// The block number found for the address and storage key.
block_number: BlockNumber,
@ -50,7 +50,7 @@ pub enum ProviderError {
storage_key: Box<B256>,
},
/// The block number was found for the given address, but the changeset was not found.
#[display("account change set for address {address} at block #{block_number} does not exist")]
#[error("account change set for address {address} at block #{block_number} does not exist")]
AccountChangesetNotFound {
/// Block number found for the address.
block_number: BlockNumber,
@ -58,121 +58,95 @@ pub enum ProviderError {
address: Address,
},
/// The total difficulty for a block is missing.
#[display("total difficulty not found for block #{_0}")]
#[error("total difficulty not found for block #{_0}")]
TotalDifficultyNotFound(BlockNumber),
/// when required header related data was not found but was required.
#[display("no header found for {_0:?}")]
/// When required header related data was not found but was required.
#[error("no header found for {_0:?}")]
HeaderNotFound(BlockHashOrNumber),
/// The specific transaction identified by hash or id is missing.
#[display("no transaction found for {_0:?}")]
#[error("no transaction found for {_0:?}")]
TransactionNotFound(HashOrNumber),
/// The specific receipt for a transaction identified by hash or id is missing
#[display("no receipt found for {_0:?}")]
#[error("no receipt found for {_0:?}")]
ReceiptNotFound(HashOrNumber),
/// Unable to find the best block.
#[display("best block does not exist")]
#[error("best block does not exist")]
BestBlockNotFound,
/// Unable to find the finalized block.
#[display("finalized block does not exist")]
#[error("finalized block does not exist")]
FinalizedBlockNotFound,
/// Unable to find the safe block.
#[display("safe block does not exist")]
#[error("safe block does not exist")]
SafeBlockNotFound,
/// Thrown when the cache service task dropped.
#[display("cache service task stopped")]
#[error("cache service task stopped")]
CacheServiceUnavailable,
/// Thrown when we failed to lookup a block for the pending state.
#[display("unknown block {_0}")]
#[error("unknown block {_0}")]
UnknownBlockHash(B256),
/// Thrown when we were unable to find a state for a block hash.
#[display("no state found for block {_0}")]
#[error("no state found for block {_0}")]
StateForHashNotFound(B256),
/// Thrown when we were unable to find a state for a block number.
#[display("no state found for block number {_0}")]
#[error("no state found for block number {_0}")]
StateForNumberNotFound(u64),
/// Unable to find the block number for a given transaction index.
#[display("unable to find the block number for a given transaction index")]
#[error("unable to find the block number for a given transaction index")]
BlockNumberForTransactionIndexNotFound,
/// Root mismatch.
#[display("merkle trie {_0}")]
#[error("merkle trie {_0}")]
StateRootMismatch(Box<RootMismatch>),
/// Root mismatch during unwind
#[display("unwind merkle trie {_0}")]
#[error("unwind merkle trie {_0}")]
UnwindStateRootMismatch(Box<RootMismatch>),
/// State is not available for the given block number because it is pruned.
#[display("state at block #{_0} is pruned")]
#[error("state at block #{_0} is pruned")]
StateAtBlockPruned(BlockNumber),
/// Provider does not support this particular request.
#[display("this provider does not support this request")]
#[error("this provider does not support this request")]
UnsupportedProvider,
/// Static File is not found at specified path.
#[cfg(feature = "std")]
#[display("not able to find {_0} static file at {_1:?}")]
#[error("not able to find {_0} static file at {_1:?}")]
MissingStaticFilePath(StaticFileSegment, std::path::PathBuf),
/// Static File is not found for requested block.
#[display("not able to find {_0} static file for block number {_1}")]
#[error("not able to find {_0} static file for block number {_1}")]
MissingStaticFileBlock(StaticFileSegment, BlockNumber),
/// Static File is not found for requested transaction.
#[display("unable to find {_0} static file for transaction id {_1}")]
#[error("unable to find {_0} static file for transaction id {_1}")]
MissingStaticFileTx(StaticFileSegment, TxNumber),
/// Static File is finalized and cannot be written to.
#[display("unable to write block #{_1} to finalized static file {_0}")]
#[error("unable to write block #{_1} to finalized static file {_0}")]
FinalizedStaticFile(StaticFileSegment, BlockNumber),
/// Trying to insert data from an unexpected block number.
#[display("trying to append data to {_0} as block #{_1} but expected block #{_2}")]
#[error("trying to append data to {_0} as block #{_1} but expected block #{_2}")]
UnexpectedStaticFileBlockNumber(StaticFileSegment, BlockNumber, BlockNumber),
/// Trying to insert data from an unexpected block number.
#[display("trying to append row to {_0} at index #{_1} but expected index #{_2}")]
#[error("trying to append row to {_0} at index #{_1} but expected index #{_2}")]
UnexpectedStaticFileTxNumber(StaticFileSegment, TxNumber, TxNumber),
/// Static File Provider was initialized as read-only.
#[display("cannot get a writer on a read-only environment.")]
#[error("cannot get a writer on a read-only environment.")]
ReadOnlyStaticFileAccess,
/// Consistent view error.
#[display("failed to initialize consistent view: {_0}")]
#[error("failed to initialize consistent view: {_0}")]
ConsistentView(Box<ConsistentViewError>),
/// Storage lock error.
StorageLockError(StorageLockError),
#[error(transparent)]
StorageLockError(#[from] StorageLockError),
/// Storage writer error.
UnifiedStorageWriterError(UnifiedStorageWriterError),
#[error(transparent)]
UnifiedStorageWriterError(#[from] UnifiedStorageWriterError),
/// Received invalid output from configured storage implementation.
#[error("received invalid output from storage")]
InvalidStorageOutput,
}
impl From<DatabaseError> for ProviderError {
fn from(error: DatabaseError) -> Self {
Self::Database(error)
}
}
impl From<alloy_rlp::Error> for ProviderError {
fn from(error: alloy_rlp::Error) -> Self {
Self::Rlp(error)
}
}
impl From<StorageLockError> for ProviderError {
fn from(error: StorageLockError) -> Self {
Self::StorageLockError(error)
}
}
impl From<UnifiedStorageWriterError> for ProviderError {
fn from(error: UnifiedStorageWriterError) -> Self {
Self::UnifiedStorageWriterError(error)
}
}
impl core::error::Error for ProviderError {
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
match self {
Self::Database(source) => core::error::Error::source(source),
Self::StorageLockError(source) => core::error::Error::source(source),
Self::UnifiedStorageWriterError(source) => core::error::Error::source(source),
_ => Option::None,
}
}
}
/// A root mismatch error at a given block height.
#[derive(Clone, Debug, PartialEq, Eq, Display)]
#[display("root mismatch at #{block_number} ({block_hash}): {root}")]