apl_token/
error.rs

1//! Error types
2
3use {
4    arch_program::{
5        decode_error::DecodeError,
6        msg,
7        program_error::{PrintProgramError, ProgramError},
8    },
9    num_derive::FromPrimitive,
10    thiserror::Error,
11};
12
13/// Errors that may be returned by the Token program.
14#[derive(Clone, Debug, Eq, Error, FromPrimitive, PartialEq)]
15pub enum TokenError {
16    // 0
17    /// Lamport balance below rent-exempt threshold.
18    #[error("Lamport balance below rent-exempt threshold")]
19    NotRentExempt,
20    /// Insufficient funds for the operation requested.
21    #[error("Insufficient funds")]
22    InsufficientFunds,
23    /// Invalid Mint.
24    #[error("Invalid Mint")]
25    InvalidMint,
26    /// Account not associated with this Mint.
27    #[error("Account not associated with this Mint")]
28    MintMismatch,
29    /// Owner does not match.
30    #[error("Owner does not match")]
31    OwnerMismatch,
32
33    // 5
34    /// This token's supply is fixed and new tokens cannot be minted.
35    #[error("Fixed supply")]
36    FixedSupply,
37    /// The account cannot be initialized because it is already being used.
38    #[error("Already in use")]
39    AlreadyInUse,
40    /// Invalid number of provided signers.
41    #[error("Invalid number of provided signers")]
42    InvalidNumberOfProvidedSigners,
43    /// Invalid number of required signers.
44    #[error("Invalid number of required signers")]
45    InvalidNumberOfRequiredSigners,
46    /// State is uninitialized.
47    #[error("State is uninitialized")]
48    UninitializedState,
49
50    // 10
51    /// Instruction does not support native tokens
52    #[error("Instruction does not support native tokens")]
53    NativeNotSupported,
54    /// Non-native account can only be closed if its balance is zero
55    #[error("Non-native account can only be closed if its balance is zero")]
56    NonNativeHasBalance,
57    /// Invalid instruction
58    #[error("Invalid instruction")]
59    InvalidInstruction,
60    /// State is invalid for requested operation.
61    #[error("State is invalid for requested operation")]
62    InvalidState,
63    /// Operation overflowed
64    #[error("Operation overflowed")]
65    Overflow,
66
67    // 15
68    /// Account does not support specified authority type.
69    #[error("Account does not support specified authority type")]
70    AuthorityTypeNotSupported,
71    /// This token mint cannot freeze accounts.
72    #[error("This token mint cannot freeze accounts")]
73    MintCannotFreeze,
74    /// Account is frozen; all account operations will fail
75    #[error("Account is frozen")]
76    AccountFrozen,
77    /// Mint decimals mismatch between the client and mint
78    #[error("The provided decimals value different from the Mint decimals")]
79    MintDecimalsMismatch,
80    /// Instruction does not support non-native tokens
81    #[error("Instruction does not support non-native tokens")]
82    NonNativeNotSupported,
83}
84impl From<TokenError> for ProgramError {
85    fn from(e: TokenError) -> Self {
86        ProgramError::Custom(e as u32)
87    }
88}
89impl<T> DecodeError<T> for TokenError {
90    fn type_of() -> &'static str {
91        "TokenError"
92    }
93}
94
95impl PrintProgramError for TokenError {
96    fn print<E>(&self)
97    where
98        E: 'static
99            + std::error::Error
100            + DecodeError<E>
101            + PrintProgramError
102            + num_traits::FromPrimitive,
103    {
104        match self {
105            TokenError::NotRentExempt => msg!("Error: Lamport balance below rent-exempt threshold"),
106            TokenError::InsufficientFunds => msg!("Error: insufficient funds"),
107            TokenError::InvalidMint => msg!("Error: Invalid Mint"),
108            TokenError::MintMismatch => msg!("Error: Account not associated with this Mint"),
109            TokenError::OwnerMismatch => msg!("Error: owner does not match"),
110            TokenError::FixedSupply => msg!("Error: the total supply of this token is fixed"),
111            TokenError::AlreadyInUse => msg!("Error: account or token already in use"),
112            TokenError::InvalidNumberOfProvidedSigners => {
113                msg!("Error: Invalid number of provided signers")
114            }
115            TokenError::InvalidNumberOfRequiredSigners => {
116                msg!("Error: Invalid number of required signers")
117            }
118            TokenError::UninitializedState => msg!("Error: State is uninitialized"),
119            TokenError::NativeNotSupported => {
120                msg!("Error: Instruction does not support native tokens")
121            }
122            TokenError::NonNativeHasBalance => {
123                msg!("Error: Non-native account can only be closed if its balance is zero")
124            }
125            TokenError::InvalidInstruction => msg!("Error: Invalid instruction"),
126            TokenError::InvalidState => msg!("Error: Invalid account state for operation"),
127            TokenError::Overflow => msg!("Error: Operation overflowed"),
128            TokenError::AuthorityTypeNotSupported => {
129                msg!("Error: Account does not support specified authority type")
130            }
131            TokenError::MintCannotFreeze => msg!("Error: This token mint cannot freeze accounts"),
132            TokenError::AccountFrozen => msg!("Error: Account is frozen"),
133            TokenError::MintDecimalsMismatch => {
134                msg!("Error: decimals different from the Mint decimals")
135            }
136            TokenError::NonNativeNotSupported => {
137                msg!("Error: Instruction does not support non-native tokens")
138            }
139        }
140    }
141}