reth_primitives_traits/block/error.rs
1//! Error types for the `block` module.
2
3use crate::transaction::signed::RecoveryError;
4
5/// Type alias for [`BlockRecoveryError`] with a [`SealedBlock`](crate::SealedBlock) value.
6///
7/// This error type is specifically used when recovering a sealed block fails.
8/// It contains the original sealed block that could not be recovered, allowing
9/// callers to inspect the problematic block or attempt recovery with different
10/// parameters.
11///
12/// # Example
13///
14/// ```rust
15/// use alloy_consensus::{Block, BlockBody, Header, Signed, TxEnvelope, TxLegacy};
16/// use alloy_primitives::{Signature, B256};
17/// use reth_primitives_traits::{block::error::SealedBlockRecoveryError, SealedBlock};
18///
19/// // Create a simple block for demonstration
20/// let header = Header::default();
21/// let tx = TxLegacy::default();
22/// let signed_tx = Signed::new_unchecked(tx, Signature::test_signature(), B256::ZERO);
23/// let envelope = TxEnvelope::Legacy(signed_tx);
24/// let body = BlockBody { transactions: vec![envelope], ommers: vec![], withdrawals: None };
25/// let block = Block::new(header, body);
26/// let sealed_block = SealedBlock::new_unchecked(block, B256::ZERO);
27///
28/// // Simulate a block recovery operation that fails
29/// let block_recovery_result: Result<(), SealedBlockRecoveryError<_>> =
30/// Err(SealedBlockRecoveryError::new(sealed_block));
31///
32/// // When block recovery fails, you get the error with the original block
33/// let error = block_recovery_result.unwrap_err();
34/// let failed_block = error.into_inner();
35/// // Now you can inspect the failed block or try recovery again
36/// ```
37pub type SealedBlockRecoveryError<B> = BlockRecoveryError<crate::SealedBlock<B>>;
38
39/// Error when recovering a block from [`SealedBlock`](crate::SealedBlock) to
40/// [`RecoveredBlock`](crate::RecoveredBlock).
41///
42/// This error is returned when the block recovery fails and contains the erroneous block, because
43/// recovering a block takes ownership of the block.
44#[derive(Debug, Clone, thiserror::Error)]
45#[error("Failed to recover the block")]
46pub struct BlockRecoveryError<T>(pub T);
47
48impl<T> BlockRecoveryError<T> {
49 /// Create a new error.
50 pub const fn new(inner: T) -> Self {
51 Self(inner)
52 }
53
54 /// Unwrap the error and return the original value.
55 pub fn into_inner(self) -> T {
56 self.0
57 }
58}
59
60impl<T> From<BlockRecoveryError<T>> for RecoveryError
61where
62 T: core::fmt::Debug + Send + Sync + 'static,
63{
64 fn from(err: BlockRecoveryError<T>) -> Self {
65 Self::from_source(err)
66 }
67}