safe_token/
error.rs

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