miden_client/rpc/
errors.rs1use alloc::boxed::Box;
2use alloc::string::{String, ToString};
3use core::error::Error;
4use core::num::TryFromIntError;
5
6use miden_objects::NoteError;
7use miden_objects::account::AccountId;
8use miden_objects::crypto::merkle::MerkleError;
9use miden_objects::note::NoteId;
10use miden_objects::utils::DeserializationError;
11use thiserror::Error;
12
13use super::NodeRpcClientEndpoint;
14
15#[derive(Debug, Error)]
19pub enum RpcError {
20 #[error("accept header validation failed: {0}")]
21 AcceptHeaderError(#[from] AcceptHeaderError),
22 #[error("rpc api response contained an update for a private account: {0}")]
23 AccountUpdateForPrivateAccountReceived(AccountId),
24 #[error("failed to connect to the api server: {0}")]
25 ConnectionError(#[source] Box<dyn Error + Send + Sync + 'static>),
26 #[error("failed to deserialize rpc data: {0}")]
27 DeserializationError(String),
28 #[error("rpc api response missing an expected field: {0}")]
29 ExpectedDataMissing(String),
30 #[error("rpc api response is invalid: {0}")]
31 InvalidResponse(String),
32 #[error("grpc request failed for {endpoint}: {error_kind}")]
33 GrpcError {
34 endpoint: NodeRpcClientEndpoint,
35 error_kind: GrpcError,
36 #[source]
37 source: Option<Box<dyn Error + Send + Sync + 'static>>,
38 },
39 #[error("note with id {0} was not found")]
40 NoteNotFound(NoteId),
41}
42
43impl From<DeserializationError> for RpcError {
44 fn from(err: DeserializationError) -> Self {
45 Self::DeserializationError(err.to_string())
46 }
47}
48
49impl From<NoteError> for RpcError {
50 fn from(err: NoteError) -> Self {
51 Self::DeserializationError(err.to_string())
52 }
53}
54
55impl From<RpcConversionError> for RpcError {
56 fn from(err: RpcConversionError) -> Self {
57 Self::DeserializationError(err.to_string())
58 }
59}
60
61#[derive(Debug, Error)]
65pub enum RpcConversionError {
66 #[error("failed to deserialize: {0}")]
67 DeserializationError(#[from] DeserializationError),
68 #[error("value is not in the range 0..modulus")]
69 NotAValidFelt,
70 #[error("note error")]
71 NoteTypeError(#[from] NoteError),
72 #[error("merkle error")]
73 MerkleError(#[from] MerkleError),
74 #[error("failed to convert rpc data: {0}")]
75 InvalidField(String),
76 #[error("failed to convert int")]
77 InvalidInt(#[from] TryFromIntError),
78 #[error("field `{field_name}` expected to be present in protobuf representation of {entity}")]
79 MissingFieldInProtobufRepresentation {
80 entity: &'static str,
81 field_name: &'static str,
82 },
83}
84
85#[derive(Debug, Error)]
90pub enum GrpcError {
91 #[error("resource not found")]
92 NotFound,
93 #[error("invalid request parameters")]
94 InvalidArgument,
95 #[error("permission denied")]
96 PermissionDenied,
97 #[error("resource already exists")]
98 AlreadyExists,
99 #[error("resource exhausted or rate limited")]
100 ResourceExhausted,
101 #[error("precondition failed")]
102 FailedPrecondition,
103 #[error("operation was cancelled")]
104 Cancelled,
105 #[error("deadline exceeded")]
106 DeadlineExceeded,
107 #[error("service unavailable")]
108 Unavailable,
109 #[error("internal server error")]
110 Internal,
111 #[error("unimplemented method")]
112 Unimplemented,
113 #[error("unauthenticated request")]
114 Unauthenticated,
115 #[error("operation was aborted")]
116 Aborted,
117 #[error("operation was attempted past the valid range")]
118 OutOfRange,
119 #[error("unrecoverable data loss or corruption")]
120 DataLoss,
121 #[error("unknown error: {0}")]
122 Unknown(String),
123}
124
125impl GrpcError {
126 pub fn from_code(code: i32, message: Option<String>) -> Self {
129 match code {
130 1 => Self::Cancelled,
131 2 => Self::Unknown(message.unwrap_or_default()),
132 3 => Self::InvalidArgument,
133 4 => Self::DeadlineExceeded,
134 5 => Self::NotFound,
135 6 => Self::AlreadyExists,
136 7 => Self::PermissionDenied,
137 8 => Self::ResourceExhausted,
138 9 => Self::FailedPrecondition,
139 10 => Self::Aborted,
140 11 => Self::OutOfRange,
141 12 => Self::Unimplemented,
142 13 => Self::Internal,
143 14 => Self::Unavailable,
144 15 => Self::DataLoss,
145 16 => Self::Unauthenticated,
146 _ => Self::Unknown(
147 message.unwrap_or_else(|| format!("Unknown gRPC status code: {code}")),
148 ),
149 }
150 }
151}
152
153#[derive(Debug, Error)]
161pub enum AcceptHeaderError {
162 #[error("server rejected request - please check your version and network settings")]
163 NoSupportedMediaRange,
164 #[error("server rejected request - parsing error: {0}")]
165 ParsingError(String),
166}
167
168impl AcceptHeaderError {
169 pub fn try_from_message(message: &str) -> Option<Self> {
171 if message.contains(
173 "server does not support any of the specified application/vnd.miden content types",
174 ) {
175 return Some(Self::NoSupportedMediaRange);
176 }
177 if message.contains("genesis value failed to parse")
178 || message.contains("version value failed to parse")
179 {
180 return Some(Self::ParsingError(message.to_string()));
181 }
182 None
183 }
184}