Skip to main content

neo_solana_compat/
program_error.rs

1//! Program error types for Solana compatibility
2
3use core::fmt;
4
5/// Reasons the program may fail
6#[derive(Clone, Copy, Debug, Eq, PartialEq)]
7pub enum ProgramError {
8    /// Custom program error
9    Custom(u32),
10    /// Invalid argument provided
11    InvalidArgument,
12    /// Invalid instruction data
13    InvalidInstructionData,
14    /// Invalid account data
15    InvalidAccountData,
16    /// Account data too small
17    AccountDataTooSmall,
18    /// Insufficient funds
19    InsufficientFunds,
20    /// Incorrect program id
21    IncorrectProgramId,
22    /// Missing required signature
23    MissingRequiredSignature,
24    /// Account already initialized
25    AccountAlreadyInitialized,
26    /// Uninitialized account
27    UninitializedAccount,
28    /// Not enough account keys
29    NotEnoughAccountKeys,
30    /// Account borrow failed
31    AccountBorrowFailed,
32    /// Max seed length exceeded
33    MaxSeedLengthExceeded,
34    /// Invalid seeds
35    InvalidSeeds,
36    /// Borsh IO error
37    BorshIoError,
38    /// Account not rent exempt
39    AccountNotRentExempt,
40    /// Unsupported sysvar
41    UnsupportedSysvar,
42    /// Illegal owner
43    IllegalOwner,
44    /// Max accounts data size exceeded
45    MaxAccountsDataSizeExceeded,
46    /// Invalid reentrancy
47    InvalidReentrancy,
48}
49
50impl ProgramError {
51    /// Convert to u64 error code
52    pub const fn to_u64(&self) -> u64 {
53        match self {
54            ProgramError::Custom(error) => *error as u64,
55            ProgramError::InvalidArgument => 1,
56            ProgramError::InvalidInstructionData => 2,
57            ProgramError::InvalidAccountData => 3,
58            ProgramError::AccountDataTooSmall => 4,
59            ProgramError::InsufficientFunds => 5,
60            ProgramError::IncorrectProgramId => 6,
61            ProgramError::MissingRequiredSignature => 7,
62            ProgramError::AccountAlreadyInitialized => 8,
63            ProgramError::UninitializedAccount => 9,
64            ProgramError::NotEnoughAccountKeys => 10,
65            ProgramError::AccountBorrowFailed => 11,
66            ProgramError::MaxSeedLengthExceeded => 12,
67            ProgramError::InvalidSeeds => 13,
68            ProgramError::BorshIoError => 14,
69            ProgramError::AccountNotRentExempt => 15,
70            ProgramError::UnsupportedSysvar => 16,
71            ProgramError::IllegalOwner => 17,
72            ProgramError::MaxAccountsDataSizeExceeded => 18,
73            ProgramError::InvalidReentrancy => 19,
74        }
75    }
76}
77
78impl fmt::Display for ProgramError {
79    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
80        match self {
81            ProgramError::Custom(code) => write!(f, "Custom error: {}", code),
82            _ => write!(f, "{:?}", self),
83        }
84    }
85}
86
87impl From<u64> for ProgramError {
88    fn from(error: u64) -> Self {
89        match error {
90            1 => ProgramError::InvalidArgument,
91            2 => ProgramError::InvalidInstructionData,
92            3 => ProgramError::InvalidAccountData,
93            4 => ProgramError::AccountDataTooSmall,
94            5 => ProgramError::InsufficientFunds,
95            6 => ProgramError::IncorrectProgramId,
96            7 => ProgramError::MissingRequiredSignature,
97            8 => ProgramError::AccountAlreadyInitialized,
98            9 => ProgramError::UninitializedAccount,
99            10 => ProgramError::NotEnoughAccountKeys,
100            11 => ProgramError::AccountBorrowFailed,
101            12 => ProgramError::MaxSeedLengthExceeded,
102            13 => ProgramError::InvalidSeeds,
103            14 => ProgramError::BorshIoError,
104            15 => ProgramError::AccountNotRentExempt,
105            16 => ProgramError::UnsupportedSysvar,
106            17 => ProgramError::IllegalOwner,
107            18 => ProgramError::MaxAccountsDataSizeExceeded,
108            19 => ProgramError::InvalidReentrancy,
109            _ => ProgramError::Custom(error as u32),
110        }
111    }
112}