casper_erc20_crate/
error.rs

1//! Error handling on the casper platform.
2use casper_types::ApiError;
3
4/// Errors which can be returned by the library.
5///
6/// When an `Error` is returned from a smart contract, it is converted to an [`ApiError::User`].
7///
8/// Where a smart contract consuming this library needs to define further error variants, it can
9/// return those via the [`Error::User`] variant or equivalently via the [`ApiError::User`]
10/// variant.
11///
12/// Such a user error should be in the range `[0..(u16::MAX - 4)]` (i.e. [0, 65532]) to avoid
13/// conflicting with the other `Error` variants.
14pub enum Error {
15    /// ERC20 contract called from within an invalid context.
16    InvalidContext,
17    /// Spender does not have enough balance.
18    InsufficientBalance,
19    /// Spender does not have enough allowance approved.
20    InsufficientAllowance,
21    /// Operation would cause an integer overflow.
22    Overflow,
23    /// User error.
24    User(u16),
25}
26
27const ERROR_INVALID_CONTEXT: u16 = u16::MAX;
28const ERROR_INSUFFICIENT_BALANCE: u16 = u16::MAX - 1;
29const ERROR_INSUFFICIENT_ALLOWANCE: u16 = u16::MAX - 2;
30const ERROR_OVERFLOW: u16 = u16::MAX - 3;
31
32impl From<Error> for ApiError {
33    fn from(error: Error) -> Self {
34        let user_error = match error {
35            Error::InvalidContext => ERROR_INVALID_CONTEXT,
36            Error::InsufficientBalance => ERROR_INSUFFICIENT_BALANCE,
37            Error::InsufficientAllowance => ERROR_INSUFFICIENT_ALLOWANCE,
38            Error::Overflow => ERROR_OVERFLOW,
39            Error::User(user_error) => user_error,
40        };
41        ApiError::User(user_error)
42    }
43}