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    /// Error code not recognized by this client version. This can happen if the node is newer than
32    /// the client and has added new error variants.
33    #[error("unknown error code {code}: {message}")]
34    Unknown { code: u8, message: String },
35}
36
37impl GetAccountError {
38    pub fn from_code(code: u8, message: &str) -> Self {
39        match code {
40            0 => Self::Internal,
41            1 => Self::DeserializationFailed,
42            2 => Self::AccountNotFound,
43            3 => Self::AccountNotPublic,
44            4 => Self::UnknownBlock,
45            5 => Self::BlockPruned,
46            _ => Self::Unknown { code, message: String::from(message) },
47        }
48    }
49}
50
51// REGISTER ACCOUNT ERROR
52// ================================================================================================
53
54/// Reason the node rejected a registration.
55#[derive(Debug, Clone, PartialEq, Eq, Error)]
56pub enum RegisterAccountError {
57    #[error("invitation code does not exist")]
58    InvitationNotFound,
59    #[error("the invitation code or the account is already registered")]
60    AlreadyRegistered,
61    /// The request was malformed. The account ID was missing or unreadable, or the invitation code
62    /// was empty.
63    #[error("invalid registration request: {0}")]
64    InvalidRequest(String),
65}
66
67impl RegisterAccountError {
68    /// Returns the typed error for the status codes the node uses to reject a registration.
69    ///
70    /// Returns `None` for every other code, because those report a transport or node-side failure
71    /// rather than a decision about the registration.
72    pub fn from_grpc_error(error_kind: &GrpcError, message: &str) -> Option<Self> {
73        match error_kind {
74            GrpcError::NotFound => Some(Self::InvitationNotFound),
75            GrpcError::AlreadyExists => Some(Self::AlreadyRegistered),
76            GrpcError::InvalidArgument => Some(Self::InvalidRequest(String::from(message))),
77            _ => None,
78        }
79    }
80}