near_api/
errors.rs

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
use near_jsonrpc_client::{
    errors::JsonRpcError,
    methods::{query::RpcQueryRequest, tx::RpcTransactionError, RpcMethod},
};
use near_jsonrpc_primitives::types::query::QueryResponseKind;

#[derive(thiserror::Error, Debug)]
pub enum QueryCreationError {
    #[error("Staking pool factory account ID is not defined in the network config")]
    StakingPoolFactoryNotDefined,
}

#[derive(thiserror::Error, Debug)]
pub enum QueryError<Method: RpcMethod>
where
    Method::Error: std::fmt::Debug + std::fmt::Display,
{
    #[error(transparent)]
    QueryCreationError(#[from] QueryCreationError),
    #[error("Unexpected response kind: expected {expected} type, but got {got:?}")]
    UnexpectedResponse {
        expected: &'static str,
        got: QueryResponseKind,
    },
    #[error("Failed to deserialize response: {0}")]
    DeserializeError(#[from] serde_json::Error),
    #[error(transparent)]
    JsonRpcError(#[from] JsonRpcError<Method::Error>),
    #[error("Internal error: failed to get response. Please submit a bug ticket")]
    InternalErrorNoResponse,
}

#[derive(thiserror::Error, Debug)]
pub enum MetaSignError {
    // near_primitives::action::delegate::IsDelegateAction is private, so we redefined it here
    #[error("Attempted to construct NonDelegateAction from Action::Delegate")]
    DelegateActionIsNotSupported,

    #[error(transparent)]
    SignerError(#[from] SignerError),
}

#[derive(thiserror::Error, Debug)]
pub enum SignerError {
    #[error("Public key is not available")]
    PublicKeyIsNotAvailable,
    #[error("Secret key is not available")]
    SecretKeyIsNotAvailable,
    #[error("Failed to fetch nonce: {0}")]
    FetchNonceError(#[from] QueryError<RpcQueryRequest>),

    #[cfg(feature = "ledger")]
    #[error(transparent)]
    LedgerError(#[from] LedgerError),
}

#[derive(thiserror::Error, Debug)]
pub enum SecretError {
    #[error("Failed to process seed phrase: {0}")]
    BIP39Error(#[from] bip39::Error),
    #[error("Failed to derive key from seed phrase: Invalid Index")]
    DeriveKeyInvalidIndex,
}

#[derive(thiserror::Error, Debug)]
pub enum AccessKeyFileError {
    #[error("Failed to read access key file: {0}")]
    ReadError(#[from] std::io::Error),
    #[error("Failed to parse access key file: {0}")]
    ParseError(#[from] serde_json::Error),
    #[error(transparent)]
    SecretError(#[from] SecretError),
}

#[cfg(feature = "keystore")]
#[derive(thiserror::Error, Debug)]
pub enum KeyStoreError {
    #[error(transparent)]
    Keystore(#[from] keyring::Error),
    #[error("Failed to query account keys: {0}")]
    QueryError(#[from] QueryError<RpcQueryRequest>),
    #[error("Failed to parse access key file: {0}")]
    ParseError(#[from] serde_json::Error),
    #[error(transparent)]
    SecretError(#[from] SecretError),
}

#[cfg(feature = "ledger")]
#[derive(thiserror::Error, Debug)]
pub enum LedgerError {
    #[error(
        "Buffer overflow on Ledger device occured. \
Transaction is too large for signature. \
This is resolved in https://github.com/dj8yfo/app-near-rs . \
The status is tracked in `About` section."
    )]
    BufferOverflow,
    #[error("Ledger device error: {0:?}")]
    LedgerError(near_ledger::NEARLedgerError),
    #[error("IO error: {0}")]
    IO(#[from] std::io::Error),
    #[error("Task execution error: {0}")]
    TaskExecutionError(#[from] tokio::task::JoinError),
    #[error("Signature is not expected to fail on deserialization: {0}")]
    SignatureDeserializationError(#[from] near_crypto::ParseSignatureError),
}

#[cfg(feature = "ledger")]
impl From<near_ledger::NEARLedgerError> for LedgerError {
    fn from(err: near_ledger::NEARLedgerError) -> Self {
        const SW_BUFFER_OVERFLOW: &str = "0x6990";

        match err {
            near_ledger::NEARLedgerError::APDUExchangeError(msg)
                if msg.contains(SW_BUFFER_OVERFLOW) =>
            {
                Self::BufferOverflow
            }
            near_ledger_error => Self::LedgerError(near_ledger_error),
        }
    }
}

#[derive(thiserror::Error, Debug)]
pub enum SecretBuilderkError<E: std::fmt::Debug> {
    #[error("Public key is not available")]
    PublicKeyIsNotAvailable,
    #[error(transparent)]
    SecretError(#[from] SecretError),
    #[error(transparent)]
    IO(#[from] std::io::Error),
    #[error(transparent)]
    CallbackError(E),
}

#[derive(thiserror::Error, Debug)]
pub enum BuilderError {
    #[error("Incorrect arguments: {0}")]
    IncorrectArguments(#[from] serde_json::Error),
}

#[derive(thiserror::Error, Debug)]
pub enum AccountCreationError {
    #[error(transparent)]
    BuilderError(#[from] BuilderError),

    #[error("Top-level account is not allowed")]
    TopLevelAccountIsNotAllowed,

    #[error("Linkdrop is not defined in the network config")]
    LinkdropIsNotDefined,

    #[error("Account should be created as a subaccount of the signer or linkdrop account")]
    AccountShouldBeSubaccountOfSignerOrLinkdrop,
}

#[derive(thiserror::Error, Debug)]
pub enum FaucetError {
    #[error("The <{0}> network config does not have a defined faucet (helper service) that can sponsor the creation of an account.")]
    FaucetIsNotDefined(String),
    #[error("Failed to send message: {0}")]
    SendError(#[from] reqwest::Error),
}

#[derive(Debug, Clone, thiserror::Error, PartialEq, Eq)]
pub enum DecimalNumberParsingError {
    #[error("Invalid number: {0}")]
    InvalidNumber(String),
    #[error("Too long whole part: {0}")]
    LongWhole(String),
    #[error("Too long fractional part: {0}")]
    LongFractional(String),
}

#[derive(thiserror::Error, Debug)]
pub enum ExecuteTransactionError {
    #[error("Transaction validation error: {0}")]
    ValidationError(#[from] ValidationError),
    #[error("Transaction signing error: {0}")]
    SignerError(#[from] SignerError),
    #[error("Meta-signing error: {0}")]
    MetaSignError(#[from] MetaSignError),
    #[error("Pre-query error: {0}")]
    PreQueryError(#[from] QueryError<RpcQueryRequest>),
    #[error("Retries exhausted. The last error is: {0}")]
    RetriesExhausted(JsonRpcError<RpcTransactionError>),
    #[deprecated(since = "0.2.1", note = "unused")]
    #[error("Transaction error: {0}")]
    CriticalTransactionError(JsonRpcError<RpcTransactionError>),
    #[error(transparent)]
    NonEmptyVecError(#[from] NonEmptyVecError),
}

#[derive(thiserror::Error, Debug)]
pub enum ExecuteMetaTransactionsError {
    #[error("Transaction validation error: {0}")]
    ValidationError(#[from] ValidationError),
    #[error("Meta-signing error: {0}")]
    SignError(#[from] MetaSignError),
    #[error("Pre-query error: {0}")]
    PreQueryError(#[from] QueryError<RpcQueryRequest>),

    #[error("Relayer is not defined in the network config")]
    RelayerIsNotDefined,

    #[error("Failed to send meta-transaction: {0}")]
    SendError(#[from] reqwest::Error),

    #[error(transparent)]
    NonEmptyVecError(#[from] NonEmptyVecError),
}

#[derive(thiserror::Error, Debug)]
pub enum FTValidatorError {
    #[error("Metadata is not provided")]
    NoMetadata,
    #[error("Decimals mismatch: expected {expected}, got {got}")]
    DecimalsMismatch { expected: u8, got: u8 },
    #[error("Storage deposit is needed")]
    StorageDepositNeeded,
}

#[derive(thiserror::Error, Debug)]
pub enum FastNearError {
    #[error("FastNear URL is not defined in the network config")]
    FastNearUrlIsNotDefined,
    #[error("Failed to send request: {0}")]
    SendError(#[from] reqwest::Error),
    #[error("Url parsing error: {0}")]
    UrlParseError(#[from] url::ParseError),
}

//TODO: it's better to have a separate errors, but for now it would be aggregated here
#[derive(thiserror::Error, Debug)]
pub enum ValidationError {
    #[error("Query error: {0}")]
    QueryError(#[from] QueryError<RpcQueryRequest>),

    #[error("Query creation error: {0}")]
    QueryBuilderError(#[from] BuilderError),

    #[error("FT Validation Error: {0}")]
    FTValidatorError(#[from] FTValidatorError),

    #[error("Account creation error: {0}")]
    AccountCreationError(#[from] AccountCreationError),
}

#[derive(thiserror::Error, Debug)]
pub enum MultiTransactionError {
    #[error(transparent)]
    NonEmptyVecError(#[from] NonEmptyVecError),

    #[error(transparent)]
    SignerError(#[from] SignerError),
    #[error("Duplicate signer")]
    DuplicateSigner,

    #[error(transparent)]
    SignedTransactionError(#[from] ExecuteTransactionError),

    #[error("Failed to send meta-transaction: {0}")]
    MetaTransactionError(#[from] ExecuteMetaTransactionsError),
}

#[derive(thiserror::Error, Debug)]
pub enum NonEmptyVecError {
    #[error("Vector is empty")]
    EmptyVector,
}