Skip to content

Commit

Permalink
error: use derive-more Error for deriving error (#10841)
Browse files Browse the repository at this point in the history
  • Loading branch information
tcoratger authored Sep 17, 2024
1 parent 30d8ec7 commit 1d0b18c
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 50 deletions.
24 changes: 4 additions & 20 deletions crates/net/p2p/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::ops::RangeInclusive;

use super::headers::client::HeadersRequest;
use derive_more::Display;
use derive_more::{Display, Error};
use reth_consensus::ConsensusError;
use reth_network_peers::WithPeerId;
use reth_network_types::ReputationChangeKind;
Expand Down Expand Up @@ -76,7 +76,7 @@ impl EthResponseValidator for RequestResult<Vec<Header>> {
/// Error variants that can happen when sending requests to a session.
///
/// Represents errors encountered when sending requests.
#[derive(Clone, Debug, Eq, PartialEq, Display)]
#[derive(Clone, Debug, Eq, PartialEq, Display, Error)]
pub enum RequestError {
/// Closed channel to the peer.
#[display("closed channel to the peer")]
Expand Down Expand Up @@ -126,14 +126,11 @@ impl From<oneshot::error::RecvError> for RequestError {
}
}

#[cfg(feature = "std")]
impl std::error::Error for RequestError {}

/// The download result type
pub type DownloadResult<T> = Result<T, DownloadError>;

/// The downloader error type
#[derive(Debug, Clone, PartialEq, Eq, Display)]
#[derive(Debug, Clone, PartialEq, Eq, Display, Error)]
pub enum DownloadError {
/* ==================== HEADER ERRORS ==================== */
/// Header validation failed.
Expand All @@ -144,6 +141,7 @@ pub enum DownloadError {
/// Number of header failing validation
number: u64,
/// The details of validation failure
#[error(source)]
error: Box<ConsensusError>,
},
/// Received an invalid tip.
Expand Down Expand Up @@ -216,20 +214,6 @@ impl From<ProviderError> for DownloadError {
}
}

#[cfg(feature = "std")]
impl std::error::Error for DownloadError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::HeaderValidation { error, .. } | Self::BodyValidation { error, .. } => {
std::error::Error::source(error)
}
Self::RequestError(error) => std::error::Error::source(error),
Self::Provider(error) => std::error::Error::source(error),
_ => None,
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
14 changes: 3 additions & 11 deletions crates/net/p2p/src/headers/error.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use derive_more::Display;
use derive_more::{Display, Error};
use reth_consensus::ConsensusError;
use reth_primitives::SealedHeader;

/// Header downloader result
pub type HeadersDownloaderResult<T> = Result<T, HeadersDownloaderError>;

/// Error variants that can happen when sending requests to a session.
#[derive(Debug, Clone, Eq, PartialEq, Display)]
#[derive(Debug, Clone, Eq, PartialEq, Display, Error)]
pub enum HeadersDownloaderError {
/// The downloaded header cannot be attached to the local head,
/// but is valid otherwise.
Expand All @@ -17,15 +17,7 @@ pub enum HeadersDownloaderError {
/// The header we attempted to attach.
header: Box<SealedHeader>,
/// The error that occurred when attempting to attach the header.
#[error(source)]
error: Box<ConsensusError>,
},
}

#[cfg(feature = "std")]
impl std::error::Error for HeadersDownloaderError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::DetachedHead { error, .. } => Some(error),
}
}
}
5 changes: 1 addition & 4 deletions crates/primitives-traits/src/integer_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ impl<'a> Arbitrary<'a> for IntegerList {
}

/// Primitives error type.
#[derive(Debug, derive_more::Display)]
#[derive(Debug, derive_more::Display, derive_more::Error)]
pub enum RoaringBitmapError {
/// The provided input is invalid.
#[display("the provided input is invalid")]
Expand All @@ -148,9 +148,6 @@ pub enum RoaringBitmapError {
FailedToDeserialize,
}

#[cfg(feature = "std")]
impl std::error::Error for RoaringBitmapError {}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
5 changes: 1 addition & 4 deletions crates/primitives/src/transaction/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl std::error::Error for InvalidTransactionError {}

/// Represents error variants that can happen when trying to convert a transaction to
/// [`PooledTransactionsElement`](crate::PooledTransactionsElement)
#[derive(Debug, Clone, Eq, PartialEq, derive_more::Display)]
#[derive(Debug, Clone, Eq, PartialEq, derive_more::Display, derive_more::Error)]
pub enum TransactionConversionError {
/// This error variant is used when a transaction cannot be converted into a
/// [`PooledTransactionsElement`](crate::PooledTransactionsElement) because it is not supported
Expand All @@ -75,9 +75,6 @@ pub enum TransactionConversionError {
UnsupportedForP2P,
}

#[cfg(feature = "std")]
impl std::error::Error for TransactionConversionError {}

/// Represents error variants than can happen when trying to convert a
/// [`TransactionSignedEcRecovered`](crate::TransactionSignedEcRecovered) transaction.
#[derive(Debug, Clone, Eq, PartialEq, derive_more::Display)]
Expand Down
12 changes: 1 addition & 11 deletions crates/storage/errors/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use reth_primitives::StaticFileSegment;

/// `UnifiedStorageWriter` related errors
/// `StorageWriter` related errors
#[derive(Clone, Debug, derive_more::Display, PartialEq, Eq)]
#[derive(Clone, Debug, derive_more::Display, PartialEq, Eq, derive_more::Error)]
pub enum UnifiedStorageWriterError {
/// Database writer is missing
#[display("Database writer is missing")]
Expand All @@ -18,16 +18,6 @@ pub enum UnifiedStorageWriterError {
Database(DatabaseError),
}

#[cfg(feature = "std")]
impl std::error::Error for UnifiedStorageWriterError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Database(source) => std::error::Error::source(source),
_ => Option::None,
}
}
}

impl From<DatabaseError> for UnifiedStorageWriterError {
fn from(error: DatabaseError) -> Self {
Self::Database(error)
Expand Down

0 comments on commit 1d0b18c

Please sign in to comment.