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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// Copyright 2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

//! Error handling in iota-client crate.

use std::fmt::{Debug, Display};

use bee_block::{output::NativeTokens, semantic::ConflictReason};
use packable::error::UnexpectedEOF;
use serde::{ser::Serializer, Serialize};

use crate::node_api::indexer::QueryParameter;

/// Type alias of `Result` in iota-client
pub type Result<T> = std::result::Result<T, Error>;

#[derive(Debug, thiserror::Error, Serialize)]
/// Error type of the iota client crate.
#[allow(clippy::large_enum_variant)]
#[serde(tag = "type", content = "error", rename_all = "camelCase")]
pub enum Error {
    /// Block dtos error
    #[error("{0}")]
    #[serde(serialize_with = "display_string")]
    ApiTypes(#[from] bee_api_types::error::Error),
    /// Blake2b256 Error
    #[error("{0}")]
    Blake2b256Error(&'static str),
    /// Block dtos error
    #[error("{0}")]
    #[serde(serialize_with = "display_string")]
    BlockDtoError(#[from] bee_block::DtoError),
    /// Block types error
    #[error("{0}")]
    #[serde(serialize_with = "display_string")]
    BlockError(#[from] bee_block::Error),
    /// The wallet account has enough funds, but split on too many outputs
    #[error("the wallet account has enough funds, but split on too many outputs: {0}, max. is 128, consolidate them")]
    ConsolidationRequired(usize),
    /// Crypto.rs error
    #[error("{0}")]
    #[serde(serialize_with = "display_string")]
    CryptoError(#[from] crypto::Error),
    /// Address not found
    #[error("address: {0} not found in range: {1}")]
    InputAddressNotFound(String, String),
    /// Invalid amount in API response
    #[error("invalid amount in API response: {0}")]
    InvalidAmount(String),
    /// Invalid BIP32 chain data
    #[error("invalid BIP32 chain data")]
    InvalidBIP32ChainData,
    /// Invalid mnemonic error
    #[error("invalid mnemonic {0}")]
    InvalidMnemonic(String),
    /// JSON error
    #[error("{0}")]
    #[serde(serialize_with = "display_string")]
    Json(#[from] serde_json::Error),
    /// Missing input for utxo chain
    #[error("missing input: {0}")]
    MissingInput(String),
    /// Missing required parameters
    #[error("must provide required parameter: {0}")]
    MissingParameter(&'static str),
    /// No input with matching ed25519 address provided
    #[error("no input with matching ed25519 address provided")]
    MissingInputWithEd25519Address,
    /// Error on API request
    #[error("node error: {0}")]
    NodeError(String),
    /// The block doesn't need to be promoted or reattached
    #[error("block ID `{0}` doesn't need to be promoted or reattached")]
    NoNeedPromoteOrReattach(String),
    /// The requested data was not found.
    #[error("the requested data {0} was not found.")]
    NotFound(String),
    /// The wallet account doesn't have enough balance
    #[error("the wallet account doesn't have enough balance. It only has {found}, required is {required}")]
    NotEnoughBalance {
        /// The amount found in the balance.
        found: u64,
        /// The required amount.
        required: u64,
    },
    /// The wallet account doesn't have any inputs found
    #[error("no inputs found")]
    NoInputs,
    /// The wallet account doesn't have enough native tokens
    #[error("the wallet account doesn't have enough native tokens, missing: {0:?}")]
    NotEnoughNativeTokens(NativeTokens),
    /// The wallet account doesn't have enough balance for an output with the remaining native tokens.
    #[error("the wallet account doesn't have enough balance for an output with the remaining native tokens.")]
    NoBalanceForNativeTokenRemainder,
    /// Output Error
    #[error("output error: {0}")]
    OutputError(&'static str),
    /// PlaceholderSecretManager can't be used for address generation or signing
    #[error("placeholderSecretManager can't be used for address generation or signing")]
    PlaceholderSecretManager,
    /// Rw lock failed.
    #[error("rw lock failed")]
    PoisonError,
    /// PoW error
    #[error("{0}")]
    Pow(String),
    /// Prefix hex string convert error
    #[error("{0}")]
    #[serde(serialize_with = "display_string")]
    PrefixHexError(#[from] prefix_hex::Error),
    /// Error on quorum because not enough nodes are available
    #[error("not enough nodes for quorum: {available_nodes} < {minimum_threshold}")]
    QuorumPoolSizeError {
        /// The number of nodes available for quorum.
        available_nodes: usize,
        /// The minimum quorum threshold.
        minimum_threshold: usize,
    },
    /// Error on reaching quorum
    #[error("failed to reach quorum: {quorum_size} < {minimum_threshold}")]
    QuorumThresholdError {
        /// The current quorum size.
        quorum_size: usize,
        /// The minimum quorum threshold.
        minimum_threshold: usize,
    },
    /// Error from RestAPI calls with unexpected status code response
    #[error("response error with status code {code}: {text}, URL: {url}")]
    ResponseError {
        /// The status code.
        code: u16,
        /// The text from the response.
        text: String,
        /// The url of the API.
        url: String,
    },
    /// reqwest error
    #[error("{0}")]
    #[serde(serialize_with = "display_string")]
    ReqwestError(#[from] reqwest::Error),
    /// Specifically used for `TryInfo` implementations for `SecretManager`.
    #[error("cannot unwrap a SecretManager: type mismatch!")]
    SecretManagerMismatch,
    /// No node available in the healthy node pool
    #[error("no healthy node available")]
    HealthyNodePoolEmpty,
    /// Error when building tagged_data blocks
    #[error("error when building tagged_data block: {0}")]
    TaggedDataError(String),
    /// The block cannot be included into the Tangle
    #[error("block ID `{0}` couldn't get included into the Tangle")]
    TangleInclusionError(String),
    #[cfg(not(target_family = "wasm"))]
    /// Tokio task join error
    #[error("{0}")]
    #[serde(serialize_with = "display_string")]
    TaskJoinError(#[from] tokio::task::JoinError),
    /// Local time doesn't match the time of the latest milestone timestamp
    #[error(
        "local time {current_time} doesn't match the time of the latest milestone timestamp: {milestone_timestamp}"
    )]
    TimeNotSynced {
        /// The local time.
        current_time: u32,
        /// The timestamp of the latest milestone.
        milestone_timestamp: u32,
    },
    /// The semantic validation of a transaction failed.
    #[error("the semantic validation of a transaction failed with conflict reason: {} - {0:?}", *.0 as u8)]
    TransactionSemantic(ConflictReason),
    /// Unexpected API response error
    #[error("unexpected API response")]
    UnexpectedApiResponse,
    /// An indexer API request contains a query parameter not supported by the endpoint.
    #[error("an indexer API request contains a query parameter not supported by the endpoint: {0}.")]
    UnsupportedQueryParameter(QueryParameter),
    /// Unpack error
    #[error("{0}")]
    #[serde(serialize_with = "display_string")]
    UnpackError(#[from] packable::error::UnpackError<bee_block::Error, UnexpectedEOF>),
    /// URL auth error
    #[error("can't set {0} to URL")]
    UrlAuthError(&'static str),
    /// URL error
    #[error("{0}")]
    #[serde(serialize_with = "display_string")]
    UrlError(#[from] url::ParseError),
    /// URL validation error
    #[error("{0}")]
    UrlValidationError(String),

    //////////////////////////////////////////////////////////////////////
    // Ledger Nano
    //////////////////////////////////////////////////////////////////////
    /// Denied by User
    #[cfg(feature = "ledger_nano")]
    #[error("denied by user")]
    LedgerDeniedByUser,
    /// Dongle Locked
    #[cfg(feature = "ledger_nano")]
    #[error("ledger locked")]
    LedgerDongleLocked,
    /// Ledger Device not found
    #[cfg(feature = "ledger_nano")]
    #[error("ledger device not found")]
    LedgerDeviceNotFound,
    /// Ledger Essence Too Large
    #[cfg(feature = "ledger_nano")]
    #[error("ledger essence too large")]
    LedgerEssenceTooLarge,
    /// Ledger transport error
    #[cfg(feature = "ledger_nano")]
    #[error("ledger transport error")]
    LedgerMiscError,

    //////////////////////////////////////////////////////////////////////
    // MQTT
    //////////////////////////////////////////////////////////////////////
    /// Invalid MQTT topic.
    #[cfg(feature = "mqtt")]
    #[error("the MQTT topic {0} is invalid")]
    InvalidMqttTopic(String),
    /// Mqtt client error
    #[cfg(feature = "mqtt")]
    #[error("{0}")]
    #[serde(serialize_with = "display_string")]
    MqttClientError(#[from] rumqttc::ClientError),
    /// MQTT connection not found (all nodes MQTT's are disabled)
    #[cfg(feature = "mqtt")]
    #[error("mQTT connection not found (all nodes have the MQTT plugin disabled)")]
    MqttConnectionNotFound,

    //////////////////////////////////////////////////////////////////////
    // Stronghold
    //////////////////////////////////////////////////////////////////////
    /// Stronghold client error
    #[cfg(feature = "stronghold")]
    #[error("stronghold client error: {0}")]
    #[serde(serialize_with = "display_string")]
    StrongholdClient(#[from] iota_stronghold::ClientError),
    /// Invalid stronghold password.
    #[cfg(feature = "stronghold")]
    #[error("invalid stronghold password")]
    StrongholdInvalidPassword,
    /// No password has been supplied to a Stronghold vault, or it has been cleared
    #[cfg(feature = "stronghold")]
    #[error("no password has been supplied, or the key has been cleared from the memory")]
    StrongholdKeyCleared,
    /// Stronghold memory error
    #[cfg(feature = "stronghold")]
    #[error("stronghold memory error: {0}")]
    #[serde(serialize_with = "display_string")]
    StrongholdMemory(#[from] iota_stronghold::MemoryError),
    /// A mnemonic has been already stored into a Stronghold vault
    #[cfg(feature = "stronghold")]
    #[error("a mnemonic has already been stored in the Stronghold vault")]
    StrongholdMnemonicAlreadyStored,
    /// Procedure execution error from Stronghold
    #[cfg(feature = "stronghold")]
    #[error("Stronghold reported a procedure error: {0}")]
    #[serde(serialize_with = "display_string")]
    StrongholdProcedureError(#[from] iota_stronghold::procedures::ProcedureError),
}

// map most errors to a single error but there are some errors that
// need special care.
// LedgerDongleLocked: Ask the user to unlock the dongle
// LedgerDeniedByUser: The user denied a signing
// LedgerDeviceNotFound: No usable Ledger device was found
// LedgerMiscError: Everything else.
// LedgerEssenceTooLarge: Essence with bip32 input indices need more space then the internal buffer is big
#[cfg(feature = "ledger_nano")]
impl From<iota_ledger_nano::api::errors::APIError> for Error {
    fn from(error: iota_ledger_nano::api::errors::APIError) -> Self {
        log::info!("ledger error: {}", error);
        match error {
            iota_ledger_nano::api::errors::APIError::ConditionsOfUseNotSatisfied => Error::LedgerDeniedByUser,
            iota_ledger_nano::api::errors::APIError::EssenceTooLarge => Error::LedgerEssenceTooLarge,
            iota_ledger_nano::api::errors::APIError::SecurityStatusNotSatisfied => Error::LedgerDongleLocked,
            iota_ledger_nano::api::errors::APIError::TransportError => Error::LedgerDeviceNotFound,
            _ => Error::LedgerMiscError,
        }
    }
}

/// Use this to serialize Error variants that implements Debug but not Serialize
fn display_string<T, S>(value: &T, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
    T: Display,
    S: Serializer,
{
    value.to_string().serialize(serializer)
}