etopay_sdk/wallet/
error.rs

1use super::{kdbx::KdbxStorageError, share::ShareError};
2use crate::{backend::error::ApiError, types::error::TypeError, user::error::UserKvStorageError};
3use iota_sdk::types::block;
4use serde::Serialize;
5
6/// A [`core::result::Result`] with [`WalletError`] as its error variant.
7pub type Result<T> = core::result::Result<T, WalletError>;
8
9#[derive(Debug, Serialize, PartialEq, Clone, Copy)]
10/// Kind of error contained in [`WalletError`]
11pub enum ErrorKind {
12    /// You need to set the password before you can initialize the wallet.
13    MissingPassword,
14    /// You need to set / upload the recovery share before you can initialize the wallet.
15    SetRecoveryShare,
16    /// You need to use the mnemonic or create a wallet before you can use the wallet.
17    UseMnemonic,
18}
19
20/// Wrapper for wallet errors
21#[derive(thiserror::Error, Debug)]
22pub enum WalletError {
23    /// Iota client error
24    #[error("IotaClient error: {0}")]
25    IotaClient(#[from] iota_sdk::client::Error),
26
27    /// Error occurs if password is missing
28    #[error("Password is missing")]
29    MissingPassword,
30
31    /// Wrong pin or password
32    #[error("Pin or password incorrect.")]
33    WrongPinOrPassword,
34
35    /// Error occurs if the wallet is not initialized
36    #[error("Wallet init error: {0:?}")]
37    WalletNotInitialized(ErrorKind),
38
39    /// Error raises if the feature is not implemented
40    #[error("Wallet feature is not implemented")]
41    WalletFeatureNotImplemented,
42
43    /// Error raises if authentication token is outdated or invalid
44    #[error("Unauthorized: Missing Access Token")]
45    MissingAccessToken,
46
47    /// Error raises if the wallet address is empty
48    #[error("Wallet address is empty")]
49    EmptyWalletAddress,
50
51    /// Error raises if something failed to parse
52    #[error("ParseError: {0}")]
53    Parse(String),
54
55    /// Alloy RPC error
56    #[error("Alloy RPC error: {0}")]
57    Rpc(String),
58
59    /// Error occurs is the transaction amount is invalid
60    #[error("InvalidTransactionAmount: {0}")]
61    InvalidTransactionAmount(String),
62
63    /// Error occurs is the transaction is invalid
64    #[error("InvalidTransaction: {0}")]
65    InvalidTransaction(String),
66
67    /// Insufficient balance on wallet
68    #[error("InsufficientBalanceError: {0}")]
69    InsufficientBalance(String),
70
71    /// Error caused by conversions to/from Decimal and f64
72    #[error("Decimal error: {0}")]
73    Decimal(rust_decimal::Error),
74
75    /// Block error
76    #[error("Block error: {0}")]
77    Block(#[from] block::Error),
78
79    /// Iota wallet error
80    #[error("IotaWallet error: {0}")]
81    IotaWallet(#[from] iota_sdk::wallet::Error),
82
83    /// Errors related to the kdbx storage
84    #[error("KdbxStorage error: {0}")]
85    KdbxStorage(#[from] KdbxStorageError),
86
87    /// Error occurs in sdk types
88    #[error("Type errors: {0}")]
89    Type(#[from] TypeError),
90
91    /// User repository error
92    #[error("User repository error: {0}")]
93    UserRepository(#[from] UserKvStorageError),
94
95    /// Error occurred while creating or reconstructing shares
96    #[error("Share error: {0}")]
97    Share(#[from] ShareError),
98
99    /// Error occurred while handling bip39 compliant mnemonics
100    #[error("Bip39 error: {0:?}")]
101    Bip39(iota_sdk::crypto::keys::bip39::Error),
102
103    /// Error occurs in sdk backend (api)
104    #[error("BackendApi errors: {0}")]
105    BackendApi(#[from] ApiError),
106
107    /// Error creating a LocalSigner from the provided mnemonic
108    #[error("LocalSignerError: {0}")]
109    LocalSignerError(#[from] alloy::signers::local::LocalSignerError),
110
111    /// Error waiting for transaction to be included
112    #[error("PendingTransactionError: {0}")]
113    PendingTransactionError(#[from] alloy::providers::PendingTransactionError),
114
115    /// Could not convert hex to address
116    #[error("Invalid hex value: {0}")]
117    FromHexError(#[from] alloy_primitives::hex::FromHexError),
118
119    /// Alloy transport error
120    #[error("Alloy transport RPC error: {0}")]
121    AlloyTransportRpcError(#[from] alloy_json_rpc::RpcError<alloy_transport::TransportErrorKind>),
122
123    /// Error raises if transaction does not exist
124    #[error("TransactionNotFound")]
125    TransactionNotFound,
126
127    /// Error raises if value cannot be converted
128    #[error("Unable to convert: {0}")]
129    ConversionError(String),
130
131    /// Error for calling a Smart Contract
132    #[error("Contract error: {0}")]
133    Contract(#[from] alloy::contract::Error),
134
135    /// Error for decoding a Smart Contract call
136    #[error("SolidityError error: {0}")]
137    SolidityError(#[from] alloy::sol_types::Error),
138}
139
140impl From<iota_sdk::crypto::keys::bip39::Error> for WalletError {
141    fn from(value: iota_sdk::crypto::keys::bip39::Error) -> Self {
142        Self::Bip39(value)
143    }
144}
145
146impl From<rust_decimal::Error> for WalletError {
147    fn from(value: rust_decimal::Error) -> Self {
148        Self::Decimal(value)
149    }
150}