Skip to main content

miden_client/rpc/errors/node/
account.rs

1use alloc::string::String;
2
3use thiserror::Error;
4
5use crate::rpc::errors::GrpcError;
6
7// GET ACCOUNT ERROR
8// ================================================================================================
9
10// Error codes match `GetAccountErrorCode` in `miden-node/crates/rpc/src/server/api/error_codes.rs`.
11#[derive(Debug, Clone, PartialEq, Eq, Error)]
12pub enum GetAccountError {
13    /// Internal server error (code 0)
14    #[error("internal server error")]
15    Internal,
16    /// Failed to deserialize data
17    #[error("deserialization failed")]
18    DeserializationFailed,
19    /// Account was not found at the requested block
20    #[error("account not found")]
21    AccountNotFound,
22    /// Account is not public
23    #[error("account is not public")]
24    AccountNotPublic,
25    /// Requested block number is unknown
26    #[error("unknown block")]
27    UnknownBlock,
28    /// Requested block has been pruned
29    #[error("block pruned")]
30    BlockPruned,
31    /// Requested storage slot does not exist in the account at the requested block
32    #[error("storage slot not found")]
33    StorageSlotNotFound,
34    /// Requested storage slot is not a map slot
35    #[error("storage slot is not a map")]
36    StorageSlotNotMap,
37    /// Error code not recognized by this client version. This can happen if the node is newer than
38    /// the client and has added new error variants.
39    #[error("unknown error code {code}: {message}")]
40    Unknown { code: u8, message: String },
41}
42
43impl GetAccountError {
44    pub fn from_code(code: u8, message: &str) -> Self {
45        match code {
46            0 => Self::Internal,
47            1 => Self::DeserializationFailed,
48            2 => Self::AccountNotFound,
49            3 => Self::AccountNotPublic,
50            4 => Self::UnknownBlock,
51            5 => Self::BlockPruned,
52            6 => Self::StorageSlotNotFound,
53            7 => Self::StorageSlotNotMap,
54            _ => Self::Unknown { code, message: String::from(message) },
55        }
56    }
57}
58
59// REGISTER ACCOUNT ERROR
60// ================================================================================================
61
62/// Reason the node rejected a registration.
63#[derive(Debug, Clone, PartialEq, Eq, Error)]
64pub enum RegisterAccountError {
65    #[error("invitation code does not exist")]
66    InvitationNotFound,
67    #[error("the invitation code or the account is already registered")]
68    AlreadyRegistered,
69    /// The request was malformed. The account ID was missing or unreadable, or the invitation code
70    /// was empty.
71    #[error("invalid registration request: {0}")]
72    InvalidRequest(String),
73}
74
75impl RegisterAccountError {
76    /// Returns the typed error for the status codes the node uses to reject a registration.
77    ///
78    /// Returns `None` for every other code, because those report a transport or node-side failure
79    /// rather than a decision about the registration.
80    pub fn from_grpc_error(error_kind: &GrpcError, message: &str) -> Option<Self> {
81        match error_kind {
82            GrpcError::NotFound => Some(Self::InvitationNotFound),
83            GrpcError::AlreadyExists => Some(Self::AlreadyRegistered),
84            GrpcError::InvalidArgument => Some(Self::InvalidRequest(String::from(message))),
85            _ => None,
86        }
87    }
88}