miden_client/rpc/
errors.rs

1use alloc::{
2    boxed::Box,
3    string::{String, ToString},
4};
5use core::error::Error;
6
7use miden_objects::{NoteError, account::AccountId, note::NoteId, utils::DeserializationError};
8use thiserror::Error;
9
10// RPC ERROR
11// ================================================================================================
12
13#[derive(Debug, Error)]
14pub enum RpcError {
15    #[error("rpc api response contained an update for a private account: {0}")]
16    AccountUpdateForPrivateAccountReceived(AccountId),
17    #[error("failed to connect to the api server: {0}")]
18    ConnectionError(#[source] Box<dyn Error + Send + Sync + 'static>),
19    #[error("failed to deserialize rpc data: {0}")]
20    DeserializationError(String),
21    #[error("rpc api response missing an expected field: {0}")]
22    ExpectedDataMissing(String),
23    #[error("rpc api response is invalid: {0}")]
24    InvalidResponse(String),
25    #[error("note with id {0} was not found")]
26    NoteNotFound(NoteId),
27    #[error("rpc request failed for {0}: {1}")]
28    RequestError(String, String),
29}
30
31impl From<DeserializationError> for RpcError {
32    fn from(err: DeserializationError) -> Self {
33        Self::DeserializationError(err.to_string())
34    }
35}
36
37impl From<NoteError> for RpcError {
38    fn from(err: NoteError) -> Self {
39        Self::DeserializationError(err.to_string())
40    }
41}
42
43impl From<RpcConversionError> for RpcError {
44    fn from(err: RpcConversionError) -> Self {
45        Self::DeserializationError(err.to_string())
46    }
47}
48
49// RPC CONVERSION ERROR
50// ================================================================================================
51
52#[derive(Debug, Error)]
53pub enum RpcConversionError {
54    #[error("value is not in the range 0..modulus")]
55    NotAValidFelt,
56    #[error("invalid note type value")]
57    NoteTypeError(#[from] NoteError),
58    #[error("failed to convert rpc data: {0}")]
59    InvalidField(String),
60    #[error("field `{field_name}` expected to be present in protobuf representation of {entity}")]
61    MissingFieldInProtobufRepresentation {
62        entity: &'static str,
63        field_name: &'static str,
64    },
65}