typhoon_errors/
error_code.rs

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