typhoon_errors/
error_code.rs1use pinocchio::program_error::{ProgramError, ToStr};
2
3#[derive(Debug, PartialEq, Eq)]
4pub enum ErrorCode {
5 InvalidProgramExecutable = 100,
6 AccountNotInitialized,
7 AccountNotMutable,
8 AccountNotSigner,
9 AccountOwnedByWrongProgram,
10 AccountDiscriminatorMismatch,
11 HasOneConstraint,
12 TryingToInitPayerAsProgramAccount,
13 TokenConstraintViolated,
14 BufferFull,
15}
16
17impl TryFrom<u32> for ErrorCode {
18 type Error = ProgramError;
19
20 #[inline(always)]
21 fn try_from(value: u32) -> Result<Self, Self::Error> {
22 match value {
23 100 => Ok(ErrorCode::InvalidProgramExecutable),
24 101 => Ok(ErrorCode::AccountNotInitialized),
25 102 => Ok(ErrorCode::AccountNotMutable),
26 103 => Ok(ErrorCode::AccountNotSigner),
27 104 => Ok(ErrorCode::AccountOwnedByWrongProgram),
28 105 => Ok(ErrorCode::AccountDiscriminatorMismatch),
29 106 => Ok(ErrorCode::HasOneConstraint),
30 107 => Ok(ErrorCode::TryingToInitPayerAsProgramAccount),
31 108 => Ok(ErrorCode::TokenConstraintViolated),
32 109 => Ok(ErrorCode::BufferFull),
33 _ => Err(ProgramError::InvalidArgument),
34 }
35 }
36}
37
38impl From<ErrorCode> for ProgramError {
39 #[inline(always)]
40 fn from(e: ErrorCode) -> Self {
41 ProgramError::Custom(e as u32)
42 }
43}
44
45impl ToStr for ErrorCode {
46 fn to_str<E>(&self) -> &'static str
47 where
48 E: 'static + ToStr + TryFrom<u32>,
49 {
50 match self {
51 ErrorCode::InvalidProgramExecutable => "Error: Program is not executable",
52 ErrorCode::AccountNotInitialized => "Error: Account is not initialized yet",
53 ErrorCode::AccountNotMutable => "Error: The given account is not mutable",
54 ErrorCode::AccountNotSigner => "Error: Account is not a signer",
55 ErrorCode::AccountOwnedByWrongProgram => {
56 "Error: The current owner of this account is not the expected one"
57 }
58 ErrorCode::AccountDiscriminatorMismatch => {
59 "Error: Discriminator did not match what was expected"
60 }
61 ErrorCode::HasOneConstraint => "Error: has_one constraint violated",
62 ErrorCode::TryingToInitPayerAsProgramAccount => {
63 "Error: Cannot initialize a program account with the payer account"
64 }
65 ErrorCode::TokenConstraintViolated => "Error: Token constraint was violated",
66 ErrorCode::BufferFull => "Error: Buffer is full",
67 }
68 }
69}