1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use alloc::{
    string::{String, ToString},
    vec::Vec,
};
use core::fmt;

use miden_objects::{
    accounts::AccountId, notes::NoteId, AccountError, AssetError, NoteError, TransactionScriptError,
};
use miden_tx::{
    utils::{DeserializationError, HexParseError},
    TransactionExecutorError, TransactionProverError,
};

use crate::{
    notes::NoteScreenerError,
    rpc::RpcError,
    store::StoreError,
    transactions::{
        request::TransactionRequestError, script_builder::TransactionScriptBuilderError,
    },
};

// CLIENT ERROR
// ================================================================================================

#[derive(Debug)]
pub enum ClientError {
    AccountError(AccountError),
    AssetError(AssetError),
    DataDeserializationError(DeserializationError),
    NoteNotFoundOnChain(NoteId),
    HexParseError(HexParseError),
    ImportNewAccountWithoutSeed,
    MissingOutputNotes(Vec<NoteId>),
    NoteError(NoteError),
    NoteImportError(String),
    NoteRecordError(String),
    NoConsumableNoteForAccount(AccountId),
    RpcError(RpcError),
    NoteScreenerError(NoteScreenerError),
    StoreError(StoreError),
    TransactionExecutorError(TransactionExecutorError),
    TransactionProvingError(TransactionProverError),
    TransactionRequestError(TransactionRequestError),
    TransactionScriptBuilderError(TransactionScriptBuilderError),
    TransactionScriptError(TransactionScriptError),
}

impl fmt::Display for ClientError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ClientError::AccountError(err) => write!(f, "Account error: {err}"),
            ClientError::AssetError(err) => write!(f, "Asset error: {err}"),
            ClientError::DataDeserializationError(err) => {
                write!(f, "Data deserialization error: {err}")
            },
            ClientError::NoteNotFoundOnChain(note_id) => {
                write!(f, "The note with ID {note_id} doesn't exist in the chain")
            },
            ClientError::HexParseError(err) => write!(f, "Error turning array to Digest: {err}"),
            ClientError::ImportNewAccountWithoutSeed => write!(
                f,
                "Import account error: can't import a new account without its initial seed"
            ),
            ClientError::MissingOutputNotes(note_ids) => {
                write!(
                    f,
                    "Transaction error: The transaction did not produce the expected notes corresponding to Note IDs: {}",
                    note_ids.iter().map(|&id| id.to_hex()).collect::<Vec<_>>().join(", ")
                )
            },
            ClientError::NoConsumableNoteForAccount(account_id) => {
                write!(f, "No consumable note for account ID {}", account_id)
            },
            ClientError::NoteError(err) => write!(f, "Note error: {err}"),
            ClientError::NoteImportError(err) => write!(f, "Error importing note: {err}"),
            ClientError::NoteRecordError(err) => write!(f, "Note record error: {err}"),
            ClientError::RpcError(err) => write!(f, "RPC api error: {err}"),
            ClientError::NoteScreenerError(err) => write!(f, "Note screener error: {err}"),
            ClientError::StoreError(err) => write!(f, "Store error: {err}"),
            ClientError::TransactionExecutorError(err) => {
                write!(f, "Transaction executor error: {err}")
            },
            ClientError::TransactionProvingError(err) => {
                write!(f, "Transaction prover error: {err}")
            },
            ClientError::TransactionRequestError(err) => {
                write!(f, "Transaction request error: {err}")
            },
            ClientError::TransactionScriptBuilderError(err) => {
                write!(f, "Transaction script builder error: {err}")
            },
            ClientError::TransactionScriptError(err) => {
                write!(f, "Transaction script error: {err}")
            },
        }
    }
}

// CONVERSIONS
// ================================================================================================

impl From<AccountError> for ClientError {
    fn from(err: AccountError) -> Self {
        Self::AccountError(err)
    }
}

impl From<DeserializationError> for ClientError {
    fn from(err: DeserializationError) -> Self {
        Self::DataDeserializationError(err)
    }
}

impl From<HexParseError> for ClientError {
    fn from(err: HexParseError) -> Self {
        Self::HexParseError(err)
    }
}

impl From<NoteError> for ClientError {
    fn from(err: NoteError) -> Self {
        Self::NoteError(err)
    }
}

impl From<RpcError> for ClientError {
    fn from(err: RpcError) -> Self {
        Self::RpcError(err)
    }
}

impl From<StoreError> for ClientError {
    fn from(err: StoreError) -> Self {
        Self::StoreError(err)
    }
}

impl From<TransactionExecutorError> for ClientError {
    fn from(err: TransactionExecutorError) -> Self {
        Self::TransactionExecutorError(err)
    }
}

impl From<TransactionProverError> for ClientError {
    fn from(err: TransactionProverError) -> Self {
        Self::TransactionProvingError(err)
    }
}

impl From<NoteScreenerError> for ClientError {
    fn from(err: NoteScreenerError) -> Self {
        Self::NoteScreenerError(err)
    }
}

impl From<TransactionRequestError> for ClientError {
    fn from(err: TransactionRequestError) -> Self {
        Self::TransactionRequestError(err)
    }
}

impl From<ClientError> for String {
    fn from(err: ClientError) -> String {
        err.to_string()
    }
}

impl From<TransactionScriptBuilderError> for ClientError {
    fn from(err: TransactionScriptBuilderError) -> Self {
        Self::TransactionScriptBuilderError(err)
    }
}

#[cfg(feature = "std")]
impl std::error::Error for ClientError {}

// ID PREFIX FETCH ERROR
// ================================================================================================

/// Error when Looking for a specific ID from a partial ID
#[derive(Debug, Eq, PartialEq)]
pub enum IdPrefixFetchError {
    NoMatch(String),
    MultipleMatches(String),
}

impl fmt::Display for IdPrefixFetchError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            IdPrefixFetchError::NoMatch(id) => {
                write!(f, "No matches were found with the {id}.")
            },
            IdPrefixFetchError::MultipleMatches(id) => {
                write!(
                    f,
                    "Found more than one element for the provided {id} and only one match is expected."
                )
            },
        }
    }
}