1use core::fmt;
2use solana_instruction_error::InstructionError;
3
4#[derive(Debug, Clone, PartialEq, Eq)]
14pub enum ProgramError {
15 InvalidArgument,
18 InvalidInstructionData,
20 InvalidAccountData,
22 AccountDataTooSmall,
24 InsufficientFunds,
26 IncorrectProgramId,
28 MissingRequiredSignature,
30 AccountAlreadyInitialized,
32 UninitializedAccount,
34 MissingAccount,
36 InvalidSeeds,
38 ArithmeticOverflow,
40 AccountNotRentExempt,
42 InvalidAccountOwner,
44 IncorrectAuthority,
46 Immutable,
48 BorshIoError,
50 ComputeBudgetExceeded,
52 Custom(u32),
54
55 Runtime(String),
59}
60
61impl From<InstructionError> for ProgramError {
62 fn from(err: InstructionError) -> Self {
63 #[allow(deprecated)]
64 match err {
65 InstructionError::InvalidArgument => Self::InvalidArgument,
66 InstructionError::InvalidInstructionData => Self::InvalidInstructionData,
67 InstructionError::InvalidAccountData => Self::InvalidAccountData,
68 InstructionError::AccountDataTooSmall => Self::AccountDataTooSmall,
69 InstructionError::InsufficientFunds => Self::InsufficientFunds,
70 InstructionError::IncorrectProgramId => Self::IncorrectProgramId,
71 InstructionError::MissingRequiredSignature => Self::MissingRequiredSignature,
72 InstructionError::AccountAlreadyInitialized => Self::AccountAlreadyInitialized,
73 InstructionError::UninitializedAccount => Self::UninitializedAccount,
74 InstructionError::MissingAccount | InstructionError::NotEnoughAccountKeys => {
75 Self::MissingAccount
76 }
77 InstructionError::InvalidSeeds => Self::InvalidSeeds,
78 InstructionError::ArithmeticOverflow => Self::ArithmeticOverflow,
79 InstructionError::AccountNotRentExempt => Self::AccountNotRentExempt,
80 InstructionError::InvalidAccountOwner => Self::InvalidAccountOwner,
81 InstructionError::IncorrectAuthority => Self::IncorrectAuthority,
82 InstructionError::Immutable => Self::Immutable,
83 InstructionError::BorshIoError => Self::BorshIoError,
84 InstructionError::ComputationalBudgetExceeded => Self::ComputeBudgetExceeded,
85 InstructionError::Custom(code) => Self::Custom(code),
86 other => Self::Runtime(format!("{other:?}")),
87 }
88 }
89}
90
91impl fmt::Display for ProgramError {
92 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
93 match self {
94 Self::InvalidArgument => write!(f, "invalid argument"),
95 Self::InvalidInstructionData => write!(f, "invalid instruction data"),
96 Self::InvalidAccountData => write!(f, "invalid account data"),
97 Self::AccountDataTooSmall => write!(f, "account data too small"),
98 Self::InsufficientFunds => write!(f, "insufficient funds"),
99 Self::IncorrectProgramId => write!(f, "incorrect program id"),
100 Self::MissingRequiredSignature => write!(f, "missing required signature"),
101 Self::AccountAlreadyInitialized => write!(f, "account already initialized"),
102 Self::UninitializedAccount => write!(f, "uninitialized account"),
103 Self::MissingAccount => write!(f, "missing account"),
104 Self::InvalidSeeds => write!(f, "invalid seeds"),
105 Self::ArithmeticOverflow => write!(f, "arithmetic overflow"),
106 Self::AccountNotRentExempt => write!(f, "account not rent-exempt"),
107 Self::InvalidAccountOwner => write!(f, "invalid account owner"),
108 Self::IncorrectAuthority => write!(f, "incorrect authority"),
109 Self::Immutable => write!(f, "account is immutable"),
110 Self::BorshIoError => write!(f, "borsh serialization error"),
111 Self::ComputeBudgetExceeded => write!(f, "compute budget exceeded"),
112 Self::Custom(code) => write!(f, "custom program error: {code} ({code:#x})"),
113 Self::Runtime(msg) => write!(f, "runtime error: {msg}"),
114 }
115 }
116}