1use num_derive::FromPrimitive;
4use solana_program::{
5 decode_error::DecodeError,
6 msg,
7 program_error::{PrintProgramError, ProgramError},
8};
9use thiserror::Error;
10
11#[derive(Clone, Debug, Eq, Error, FromPrimitive, PartialEq)]
13pub enum TokenError {
14 #[error("Lamport balance below rent-exempt threshold")]
17 NotRentExempt,
18 #[error("Insufficient funds")]
20 InsufficientFunds,
21 #[error("Invalid Mint")]
23 InvalidMint,
24 #[error("Account not associated with this Mint")]
26 MintMismatch,
27 #[error("Owner does not match")]
29 OwnerMismatch,
30
31 #[error("Fixed supply")]
34 FixedSupply,
35 #[error("Already in use")]
37 AlreadyInUse,
38 #[error("Invalid number of provided signers")]
40 InvalidNumberOfProvidedSigners,
41 #[error("Invalid number of required signers")]
43 InvalidNumberOfRequiredSigners,
44 #[error("State is uninitialized")]
46 UninitializedState,
47
48 #[error("Instruction does not support native tokens")]
51 NativeNotSupported,
52 #[error("Non-native account can only be closed if its balance is zero")]
54 NonNativeHasBalance,
55 #[error("Invalid instruction")]
57 InvalidInstruction,
58 #[error("State is invalid for requested operation")]
60 InvalidState,
61 #[error("Operation overflowed")]
63 Overflow,
64
65 #[error("Account does not support specified authority type")]
68 AuthorityTypeNotSupported,
69 #[error("This token mint cannot freeze accounts")]
71 MintCannotFreeze,
72 #[error("Account is frozen")]
74 AccountFrozen,
75 #[error("The provided decimals value different from the Mint decimals")]
77 MintDecimalsMismatch,
78 #[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}