1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
use brontes_metrics::trace::types::TraceParseErrorKind;
use reth_primitives::B256;
use reth_rpc::eth::error::EthApiError;
use thiserror::Error;

/// Custom error type
#[derive(Debug, Error)]
pub enum TraceParseError {
    #[error("trace missing in block {0}")]
    TracesMissingBlock(u64),
    #[error("trace missing in transaction {0}")]
    TracesMissingTx(B256),
    #[error("empty input: {0}")]
    EmptyInput(B256),
    #[error("abi parse error: {0}")]
    AbiParseError(serde_json::Error),
    #[error("invalid function selector: {0}")]
    InvalidFunctionSelector(B256),
    #[error("abi decoding failed: {0}")]
    AbiDecodingFailed(B256),
    #[error("send error to prometheus")]
    ChannelSendError(String),
    #[error("trace missing")]
    EthApiError(EthApiError),
    #[error("alloy error {0}")]
    AlloyError(alloy_dyn_abi::Error),
    #[error(transparent)]
    Eyre(#[from] eyre::Report),
}

impl From<EthApiError> for TraceParseError {
    fn from(err: EthApiError) -> TraceParseError {
        TraceParseError::EthApiError(err)
    }
}

impl From<alloy_dyn_abi::Error> for TraceParseError {
    fn from(err: alloy_dyn_abi::Error) -> TraceParseError {
        //TraceParseError::EthApiError(err)
        TraceParseError::AlloyError(err)
    }
}

/// TODO: why don't we just use the default error here since we are litterally
/// just mapping 1-1 and dropping some state.
impl From<&TraceParseError> for TraceParseErrorKind {
    fn from(val: &TraceParseError) -> Self {
        match val {
            TraceParseError::TracesMissingBlock(_) => TraceParseErrorKind::TracesMissingBlock,
            TraceParseError::TracesMissingTx(_) => TraceParseErrorKind::TracesMissingTx,
            TraceParseError::EmptyInput(_) => TraceParseErrorKind::EmptyInput,
            TraceParseError::EthApiError(e) => match e {
                EthApiError::EmptyRawTransactionData => {
                    TraceParseErrorKind::EthApiEmptyRawTransactionData
                }
                EthApiError::FailedToDecodeSignedTransaction => {
                    TraceParseErrorKind::EthApiFailedToDecodeSignedTransaction
                }
                EthApiError::InvalidTransactionSignature => {
                    TraceParseErrorKind::EthApiInvalidTransactionSignature
                }
                EthApiError::UnknownSafeOrFinalizedBlock => {
                    TraceParseErrorKind::EthApiUnknownSafeOrFinalizedBlock
                }
                EthApiError::ExecutionTimedOut(_) => TraceParseErrorKind::EthApiExecutionTimedOut,

                EthApiError::PoolError(_) => TraceParseErrorKind::EthApiPoolError,
                EthApiError::UnknownBlockNumber => TraceParseErrorKind::EthApiUnknownBlockNumber,
                EthApiError::UnknownBlockOrTxIndex => {
                    TraceParseErrorKind::EthApiUnknownBlockOrTxIndex
                }
                EthApiError::InvalidBlockRange => TraceParseErrorKind::EthApiInvalidBlockRange,
                EthApiError::PrevrandaoNotSet => TraceParseErrorKind::EthApiPrevrandaoNotSet,
                EthApiError::ConflictingFeeFieldsInRequest => {
                    TraceParseErrorKind::EthApiConflictingFeeFieldsInRequest
                }
                EthApiError::InvalidTransaction(_) => TraceParseErrorKind::EthApiInvalidTransaction,
                EthApiError::InvalidBlockData(_) => TraceParseErrorKind::EthApiInvalidBlockData,
                EthApiError::BothStateAndStateDiffInOverride(_) => {
                    TraceParseErrorKind::EthApiBothStateAndStateDiffInOverride
                }
                EthApiError::Internal(_) => TraceParseErrorKind::EthApiInternal,
                EthApiError::Signing(_) => TraceParseErrorKind::EthApiSigning,
                EthApiError::TransactionNotFound => TraceParseErrorKind::EthApiTransactionNotFound,
                EthApiError::Unsupported(_) => TraceParseErrorKind::EthApiUnsupported,
                EthApiError::InvalidParams(_) => TraceParseErrorKind::EthApiInvalidParams,
                EthApiError::InvalidTracerConfig => TraceParseErrorKind::EthApiInvalidTracerConfig,
                EthApiError::InvalidRewardPercentiles => {
                    TraceParseErrorKind::EthApiInvalidRewardPercentiles
                }
                EthApiError::InternalEthError => TraceParseErrorKind::EthApiInternalEthError,
                EthApiError::InternalJsTracerError(_) => {
                    TraceParseErrorKind::EthApiInternalJsTracerError
                }
                _ => TraceParseErrorKind::EthApiInternalJsTracerError,
            },
            TraceParseError::AbiParseError(_) => TraceParseErrorKind::AbiParseError,
            TraceParseError::InvalidFunctionSelector(_) => {
                TraceParseErrorKind::InvalidFunctionSelector
            }
            TraceParseError::AbiDecodingFailed(_) => TraceParseErrorKind::AbiDecodingFailed,
            TraceParseError::ChannelSendError(_) => TraceParseErrorKind::ChannelSendError,
            TraceParseError::AlloyError(_) => TraceParseErrorKind::AlloyError,
            TraceParseError::Eyre(_) => TraceParseErrorKind::Eyre,
        }
    }
}