Introduce AnyError (#14294)

Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
Elijah Hampton
2025-02-07 11:08:16 +00:00
committed by GitHub
parent 8f01951891
commit b8a4468c69
3 changed files with 50 additions and 1 deletions

View File

@ -0,0 +1,41 @@
use alloc::sync::Arc;
use core::{error::Error, fmt};
/// A thread-safe cloneable wrapper for any error type.
#[derive(Clone)]
pub struct AnyError {
inner: Arc<dyn Error + Send + Sync + 'static>,
}
impl AnyError {
/// Creates a new `AnyError` wrapping the given error value.
pub fn new<E>(error: E) -> Self
where
E: Error + Send + Sync + 'static,
{
Self { inner: Arc::new(error) }
}
/// Returns a reference to the underlying error value.
pub fn as_error(&self) -> &(dyn Error + Send + Sync + 'static) {
self.inner.as_ref()
}
}
impl fmt::Debug for AnyError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.inner, f)
}
}
impl fmt::Display for AnyError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.inner, f)
}
}
impl Error for AnyError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
self.inner.source()
}
}

View File

@ -22,3 +22,6 @@ pub mod provider;
/// Writer error
pub mod writer;
/// Any error
pub mod any;

View File

@ -1,4 +1,6 @@
use crate::{db::DatabaseError, lockfile::StorageLockError, writer::UnifiedStorageWriterError};
use crate::{
any::AnyError, db::DatabaseError, lockfile::StorageLockError, writer::UnifiedStorageWriterError,
};
use alloc::{boxed::Box, string::String};
use alloy_eips::{BlockHashOrNumber, HashOrNumber};
use alloy_primitives::{Address, BlockHash, BlockNumber, TxNumber, B256};
@ -143,6 +145,9 @@ pub enum ProviderError {
/// Received invalid output from configured storage implementation.
#[error("received invalid output from storage")]
InvalidStorageOutput,
/// A wrapper for arbitrary errors requiring thread-safety and cloneability.
#[error(transparent)]
Any(#[from] AnyError),
}
impl From<alloy_rlp::Error> for ProviderError {