1use {
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#[derive(Clone, Debug, Eq, Error, FromPrimitive, PartialEq)]
15pub enum TokenError {
16 #[error("Lamport balance below rent-exempt threshold")]
19 NotRentExempt,
20 #[error("Insufficient funds")]
22 InsufficientFunds,
23 #[error("Invalid Mint")]
25 InvalidMint,
26 #[error("Account not associated with this Mint")]
28 MintMismatch,
29 #[error("Owner does not match")]
31 OwnerMismatch,
32
33 #[error("Fixed supply")]
36 FixedSupply,
37 #[error("Already in use")]
39 AlreadyInUse,
40 #[error("Invalid number of provided signers")]
42 InvalidNumberOfProvidedSigners,
43 #[error("Invalid number of required signers")]
45 InvalidNumberOfRequiredSigners,
46 #[error("State is uninitialized")]
48 UninitializedState,
49
50 #[error("Instruction does not support native tokens")]
53 NativeNotSupported,
54 #[error("Non-native account can only be closed if its balance is zero")]
56 NonNativeHasBalance,
57 #[error("Invalid instruction")]
59 InvalidInstruction,
60 #[error("State is invalid for requested operation")]
62 InvalidState,
63 #[error("Operation overflowed")]
65 Overflow,
66
67 #[error("Account does not support specified authority type")]
70 AuthorityTypeNotSupported,
71 #[error("This token mint cannot freeze accounts")]
73 MintCannotFreeze,
74 #[error("Account is frozen")]
76 AccountFrozen,
77 #[error("The provided decimals value different from the Mint decimals")]
79 MintDecimalsMismatch,
80 #[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}