Skip to main content

miden_client/rpc/errors/node/
mod.rs

1mod account;
2mod block;
3mod note;
4mod sync;
5mod transaction;
6
7pub use account::GetAccountError;
8pub use block::{GetBlockByNumberError, GetBlockHeaderError};
9pub use note::{GetNoteScriptByRootError, GetNotesByIdError};
10pub use sync::{
11    NoteSyncError,
12    SyncAccountStorageMapsError,
13    SyncAccountVaultError,
14    SyncNullifiersError,
15    SyncTransactionsError,
16};
17use thiserror::Error;
18pub use transaction::AddTransactionError;
19
20use crate::rpc::RpcEndpoint;
21
22/// Application-level error returned by the node for a specific RPC endpoint.
23///
24/// Each variant wraps a typed error parsed from the error code in the node's gRPC response.
25#[derive(Debug, Clone, PartialEq, Eq, Error)]
26pub enum EndpointError {
27    /// Error from the `SubmitProvenTransaction` endpoint
28    #[error(transparent)]
29    AddTransaction(#[from] AddTransactionError),
30    /// Error from the `GetBlockHeaderByNumber` endpoint
31    #[error(transparent)]
32    GetBlockHeader(#[from] GetBlockHeaderError),
33    /// Error from the `GetBlockByNumber` endpoint
34    #[error(transparent)]
35    GetBlockByNumber(#[from] GetBlockByNumberError),
36    /// Error from the `SyncNotes` endpoint
37    #[error(transparent)]
38    NoteSync(#[from] NoteSyncError),
39    /// Error from the `SyncNullifiers` endpoint
40    #[error(transparent)]
41    SyncNullifiers(#[from] SyncNullifiersError),
42    /// Error from the `SyncAccountVault` endpoint
43    #[error(transparent)]
44    SyncAccountVault(#[from] SyncAccountVaultError),
45    /// Error from the `SyncStorageMaps` endpoint
46    #[error(transparent)]
47    SyncStorageMaps(#[from] SyncAccountStorageMapsError),
48    /// Error from the `SyncTransactions` endpoint
49    #[error(transparent)]
50    SyncTransactions(#[from] SyncTransactionsError),
51    /// Error from the `GetNotesById` endpoint
52    #[error(transparent)]
53    GetNotesById(#[from] GetNotesByIdError),
54    /// Error from the `GetNoteScriptByRoot` endpoint
55    #[error(transparent)]
56    GetNoteScriptByRoot(#[from] GetNoteScriptByRootError),
57    /// Error from the `GetAccount` endpoint
58    #[error(transparent)]
59    GetAccount(#[from] GetAccountError),
60}
61
62/// Parses the application-level error code into a typed error for the given endpoint.
63///
64/// Returns `None` if details are empty or if the endpoint doesn't have typed errors.
65pub fn parse_node_error(
66    endpoint: &RpcEndpoint,
67    details: &[u8],
68    message: &str,
69) -> Option<EndpointError> {
70    let code = *details.first()?;
71
72    match endpoint {
73        RpcEndpoint::SubmitProvenTx => {
74            Some(EndpointError::AddTransaction(AddTransactionError::from_code(code, message)))
75        },
76        RpcEndpoint::GetBlockHeaderByNumber => {
77            Some(EndpointError::GetBlockHeader(GetBlockHeaderError::from_code(code, message)))
78        },
79        RpcEndpoint::GetBlockByNumber => {
80            Some(EndpointError::GetBlockByNumber(GetBlockByNumberError::from_code(code, message)))
81        },
82        RpcEndpoint::SyncNotes => {
83            Some(EndpointError::NoteSync(NoteSyncError::from_code(code, message)))
84        },
85        RpcEndpoint::SyncNullifiers => {
86            Some(EndpointError::SyncNullifiers(SyncNullifiersError::from_code(code, message)))
87        },
88        RpcEndpoint::SyncAccountVault => {
89            Some(EndpointError::SyncAccountVault(SyncAccountVaultError::from_code(code, message)))
90        },
91        RpcEndpoint::SyncStorageMaps => Some(EndpointError::SyncStorageMaps(
92            SyncAccountStorageMapsError::from_code(code, message),
93        )),
94        RpcEndpoint::SyncTransactions => {
95            Some(EndpointError::SyncTransactions(SyncTransactionsError::from_code(code, message)))
96        },
97        RpcEndpoint::GetNotesById => {
98            Some(EndpointError::GetNotesById(GetNotesByIdError::from_code(code, message)))
99        },
100        RpcEndpoint::GetNoteScriptByRoot => Some(EndpointError::GetNoteScriptByRoot(
101            GetNoteScriptByRootError::from_code(code, message),
102        )),
103        RpcEndpoint::GetAccount => {
104            Some(EndpointError::GetAccount(GetAccountError::from_code(code, message)))
105        },
106        // These endpoints don't have typed errors from the node
107        RpcEndpoint::SyncChainMmr
108        | RpcEndpoint::Status
109        | RpcEndpoint::GetLimits
110        | RpcEndpoint::GetNetworkNoteStatus
111        | RpcEndpoint::SubmitProvenBatch => None,
112    }
113}