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::{CheckNullifiersError, 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 `CheckNullifiers` endpoint
58    #[error(transparent)]
59    CheckNullifiers(#[from] CheckNullifiersError),
60    /// Error from the `GetAccount` endpoint
61    #[error(transparent)]
62    GetAccount(#[from] GetAccountError),
63}
64
65/// Parses the application-level error code into a typed error for the given endpoint.
66///
67/// Returns `None` if details are empty or if the endpoint doesn't have typed errors.
68pub fn parse_node_error(
69    endpoint: &RpcEndpoint,
70    details: &[u8],
71    message: &str,
72) -> Option<EndpointError> {
73    let code = *details.first()?;
74
75    match endpoint {
76        RpcEndpoint::SubmitProvenTx => {
77            Some(EndpointError::AddTransaction(AddTransactionError::from_code(code, message)))
78        },
79        RpcEndpoint::GetBlockHeaderByNumber => {
80            Some(EndpointError::GetBlockHeader(GetBlockHeaderError::from_code(code, message)))
81        },
82        RpcEndpoint::GetBlockByNumber => {
83            Some(EndpointError::GetBlockByNumber(GetBlockByNumberError::from_code(code, message)))
84        },
85        RpcEndpoint::SyncNotes => {
86            Some(EndpointError::NoteSync(NoteSyncError::from_code(code, message)))
87        },
88        RpcEndpoint::SyncNullifiers => {
89            Some(EndpointError::SyncNullifiers(SyncNullifiersError::from_code(code, message)))
90        },
91        RpcEndpoint::SyncAccountVault => {
92            Some(EndpointError::SyncAccountVault(SyncAccountVaultError::from_code(code, message)))
93        },
94        RpcEndpoint::SyncStorageMaps => Some(EndpointError::SyncStorageMaps(
95            SyncAccountStorageMapsError::from_code(code, message),
96        )),
97        RpcEndpoint::SyncTransactions => {
98            Some(EndpointError::SyncTransactions(SyncTransactionsError::from_code(code, message)))
99        },
100        RpcEndpoint::GetNotesById => {
101            Some(EndpointError::GetNotesById(GetNotesByIdError::from_code(code, message)))
102        },
103        RpcEndpoint::GetNoteScriptByRoot => Some(EndpointError::GetNoteScriptByRoot(
104            GetNoteScriptByRootError::from_code(code, message),
105        )),
106        RpcEndpoint::CheckNullifiers => {
107            Some(EndpointError::CheckNullifiers(CheckNullifiersError::from_code(code, message)))
108        },
109        RpcEndpoint::GetAccount => {
110            Some(EndpointError::GetAccount(GetAccountError::from_code(code, message)))
111        },
112        // These endpoints don't have typed errors from the node
113        RpcEndpoint::SyncChainMmr | RpcEndpoint::Status | RpcEndpoint::GetLimits => None,
114    }
115}