Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error: use derive-more Error for deriving error #10841

Merged
merged 6 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -140,7 +140,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 @@ -150,9 +150,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
Loading