1
0
Эх сурвалжийг харах

Improve token error messages in token-swap (#3357)

* Move PrintProgramError impls

* Add wrapper function

* Add PrintProgramError to use declaration
Justin Malčić 3 жил өмнө
parent
commit
7fa6e2d63c

+ 53 - 1
program/src/error.rs

@@ -1,7 +1,11 @@
 //! Error types
 
 use num_derive::FromPrimitive;
-use solana_program::{decode_error::DecodeError, program_error::ProgramError};
+use solana_program::{
+    decode_error::DecodeError,
+    msg,
+    program_error::{PrintProgramError, ProgramError},
+};
 use thiserror::Error;
 
 /// Errors that may be returned by the Token program.
@@ -85,3 +89,51 @@ impl<T> DecodeError<T> for TokenError {
         "TokenError"
     }
 }
+
+impl PrintProgramError for TokenError {
+    fn print<E>(&self)
+    where
+        E: 'static
+            + std::error::Error
+            + DecodeError<E>
+            + PrintProgramError
+            + num_traits::FromPrimitive,
+    {
+        match self {
+            TokenError::NotRentExempt => msg!("Error: Lamport balance below rent-exempt threshold"),
+            TokenError::InsufficientFunds => msg!("Error: insufficient funds"),
+            TokenError::InvalidMint => msg!("Error: Invalid Mint"),
+            TokenError::MintMismatch => msg!("Error: Account not associated with this Mint"),
+            TokenError::OwnerMismatch => msg!("Error: owner does not match"),
+            TokenError::FixedSupply => msg!("Error: the total supply of this token is fixed"),
+            TokenError::AlreadyInUse => msg!("Error: account or token already in use"),
+            TokenError::InvalidNumberOfProvidedSigners => {
+                msg!("Error: Invalid number of provided signers")
+            }
+            TokenError::InvalidNumberOfRequiredSigners => {
+                msg!("Error: Invalid number of required signers")
+            }
+            TokenError::UninitializedState => msg!("Error: State is uninitialized"),
+            TokenError::NativeNotSupported => {
+                msg!("Error: Instruction does not support native tokens")
+            }
+            TokenError::NonNativeHasBalance => {
+                msg!("Error: Non-native account can only be closed if its balance is zero")
+            }
+            TokenError::InvalidInstruction => msg!("Error: Invalid instruction"),
+            TokenError::InvalidState => msg!("Error: Invalid account state for operation"),
+            TokenError::Overflow => msg!("Error: Operation overflowed"),
+            TokenError::AuthorityTypeNotSupported => {
+                msg!("Error: Account does not support specified authority type")
+            }
+            TokenError::MintCannotFreeze => msg!("Error: This token mint cannot freeze accounts"),
+            TokenError::AccountFrozen => msg!("Error: Account is frozen"),
+            TokenError::MintDecimalsMismatch => {
+                msg!("Error: decimals different from the Mint decimals")
+            }
+            TokenError::NonNativeNotSupported => {
+                msg!("Error: Instruction does not support non-native tokens")
+            }
+        }
+    }
+}

+ 5 - 48
program/src/processor.rs

@@ -7,14 +7,12 @@ use crate::{
     state::{Account, AccountState, Mint, Multisig},
     try_ui_amount_into_amount,
 };
-use num_traits::FromPrimitive;
 use solana_program::{
     account_info::{next_account_info, AccountInfo},
-    decode_error::DecodeError,
     entrypoint::ProgramResult,
     msg,
     program::set_return_data,
-    program_error::{PrintProgramError, ProgramError},
+    program_error::ProgramError,
     program_memory::{sol_memcmp, sol_memset},
     program_option::COption,
     program_pack::{IsInitialized, Pack},
@@ -1009,57 +1007,16 @@ impl Processor {
     }
 }
 
-impl PrintProgramError for TokenError {
-    fn print<E>(&self)
-    where
-        E: 'static + std::error::Error + DecodeError<E> + PrintProgramError + FromPrimitive,
-    {
-        match self {
-            TokenError::NotRentExempt => msg!("Error: Lamport balance below rent-exempt threshold"),
-            TokenError::InsufficientFunds => msg!("Error: insufficient funds"),
-            TokenError::InvalidMint => msg!("Error: Invalid Mint"),
-            TokenError::MintMismatch => msg!("Error: Account not associated with this Mint"),
-            TokenError::OwnerMismatch => msg!("Error: owner does not match"),
-            TokenError::FixedSupply => msg!("Error: the total supply of this token is fixed"),
-            TokenError::AlreadyInUse => msg!("Error: account or token already in use"),
-            TokenError::InvalidNumberOfProvidedSigners => {
-                msg!("Error: Invalid number of provided signers")
-            }
-            TokenError::InvalidNumberOfRequiredSigners => {
-                msg!("Error: Invalid number of required signers")
-            }
-            TokenError::UninitializedState => msg!("Error: State is uninitialized"),
-            TokenError::NativeNotSupported => {
-                msg!("Error: Instruction does not support native tokens")
-            }
-            TokenError::NonNativeHasBalance => {
-                msg!("Error: Non-native account can only be closed if its balance is zero")
-            }
-            TokenError::InvalidInstruction => msg!("Error: Invalid instruction"),
-            TokenError::InvalidState => msg!("Error: Invalid account state for operation"),
-            TokenError::Overflow => msg!("Error: Operation overflowed"),
-            TokenError::AuthorityTypeNotSupported => {
-                msg!("Error: Account does not support specified authority type")
-            }
-            TokenError::MintCannotFreeze => msg!("Error: This token mint cannot freeze accounts"),
-            TokenError::AccountFrozen => msg!("Error: Account is frozen"),
-            TokenError::MintDecimalsMismatch => {
-                msg!("Error: decimals different from the Mint decimals")
-            }
-            TokenError::NonNativeNotSupported => {
-                msg!("Error: Instruction does not support non-native tokens")
-            }
-        }
-    }
-}
-
 #[cfg(test)]
 mod tests {
     use super::*;
     use crate::instruction::*;
     use serial_test::serial;
     use solana_program::{
-        account_info::IntoAccountInfo, clock::Epoch, instruction::Instruction, program_error,
+        account_info::IntoAccountInfo,
+        clock::Epoch,
+        instruction::Instruction,
+        program_error::{self, PrintProgramError},
         sysvar::rent,
     };
     use solana_sdk::account::{