Skip to main content

light_sdk_types/
error.rs

1#[cfg(all(not(feature = "std"), feature = "alloc"))]
2use alloc::string::String;
3
4use light_account_checks::error::AccountError;
5use light_compressed_account::CompressedAccountError;
6use light_hasher::HasherError;
7use thiserror::Error;
8
9pub type Result<T> = core::result::Result<T, LightSdkTypesError>;
10
11#[derive(Debug, Error, PartialEq)]
12pub enum LightSdkTypesError {
13    #[error("Address is none during initialization")]
14    InitAddressIsNone,
15    #[error("Address is none during initialization with address")]
16    InitWithAddressIsNone,
17    #[error("Output is none during initialization with address")]
18    InitWithAddressOutputIsNone,
19    #[error("Address is none during meta mutation")]
20    MetaMutAddressIsNone,
21    #[error("Input is none during meta mutation")]
22    MetaMutInputIsNone,
23    #[error("Output lamports is none during meta mutation")]
24    MetaMutOutputLamportsIsNone,
25    #[error("Output is none during meta mutation")]
26    MetaMutOutputIsNone,
27    #[error("Address is none during meta close")]
28    MetaCloseAddressIsNone,
29    #[error("Input is none during meta close")]
30    MetaCloseInputIsNone,
31    #[error("Fewer accounts than system accounts")]
32    FewerAccountsThanSystemAccounts,
33    #[error("CPI accounts index out of bounds: {0}")]
34    CpiAccountsIndexOutOfBounds(usize),
35    #[error("Invalid CPI context account")]
36    InvalidCpiContextAccount,
37    #[error("Invalid sol pool pda account")]
38    InvalidSolPoolPdaAccount,
39    #[error("CpiAccounts accounts slice starts with an invalid account. It should start with LightSystemProgram SySTEM1eSU2p4BGQfQpimFEWWSC1XDFeun3Nqzz3rT7.")]
40    InvalidCpiAccountsOffset,
41    #[error(transparent)]
42    AccountError(#[from] AccountError),
43    #[error(transparent)]
44    Hasher(#[from] HasherError),
45    // --- Variants merged from LightSdkTypesError (sdk-interface) ---
46    #[error("Constraint violation")]
47    ConstraintViolation,
48    #[error("Borsh serialization/deserialization error")]
49    Borsh,
50    #[error("Missing compression info")]
51    MissingCompressionInfo,
52    #[error("Invalid rent sponsor")]
53    InvalidRentSponsor,
54    #[cfg(feature = "alloc")]
55    #[error("Borsh IO error: {0}")]
56    BorshIo(String),
57    #[error("Read-only accounts not supported in CPI context")]
58    ReadOnlyAccountsNotSupportedInCpiContext,
59    #[error(transparent)]
60    CompressedAccountError(#[from] CompressedAccountError),
61    #[error("Account data too small")]
62    AccountDataTooSmall,
63    #[error("Invalid instruction data")]
64    InvalidInstructionData,
65    #[error("Invalid seeds")]
66    InvalidSeeds,
67    #[error("CPI failed")]
68    CpiFailed,
69    #[error("Not enough account keys")]
70    NotEnoughAccountKeys,
71    #[error("Missing required signature")]
72    MissingRequiredSignature,
73    #[error("Program error: {0}")]
74    ProgramError(u32),
75}
76
77#[cfg(feature = "anchor")]
78impl From<LightSdkTypesError> for anchor_lang::error::Error {
79    fn from(e: LightSdkTypesError) -> Self {
80        anchor_lang::error::Error::from(solana_program_error::ProgramError::Custom(u32::from(e)))
81    }
82}
83
84#[cfg(feature = "anchor")]
85impl From<LightSdkTypesError> for solana_program_error::ProgramError {
86    fn from(e: LightSdkTypesError) -> Self {
87        solana_program_error::ProgramError::Custom(u32::from(e))
88    }
89}
90
91#[cfg(feature = "anchor")]
92impl From<solana_program_error::ProgramError> for LightSdkTypesError {
93    fn from(e: solana_program_error::ProgramError) -> Self {
94        match e {
95            solana_program_error::ProgramError::InvalidAccountData => {
96                LightSdkTypesError::InvalidInstructionData
97            }
98            solana_program_error::ProgramError::BorshIoError(_) => LightSdkTypesError::Borsh,
99            solana_program_error::ProgramError::AccountBorrowFailed => {
100                LightSdkTypesError::ConstraintViolation
101            }
102            other => LightSdkTypesError::ProgramError(u64::from(other) as u32),
103        }
104    }
105}
106
107impl From<LightSdkTypesError> for u32 {
108    fn from(e: LightSdkTypesError) -> Self {
109        match e {
110            LightSdkTypesError::InitAddressIsNone => 14021,
111            LightSdkTypesError::InitWithAddressIsNone => 14022,
112            LightSdkTypesError::InitWithAddressOutputIsNone => 14023,
113            LightSdkTypesError::MetaMutAddressIsNone => 14024,
114            LightSdkTypesError::MetaMutInputIsNone => 14025,
115            LightSdkTypesError::MetaMutOutputLamportsIsNone => 14026,
116            LightSdkTypesError::MetaMutOutputIsNone => 14027,
117            LightSdkTypesError::MetaCloseAddressIsNone => 14028,
118            LightSdkTypesError::MetaCloseInputIsNone => 14029,
119            LightSdkTypesError::FewerAccountsThanSystemAccounts => 14017,
120            LightSdkTypesError::CpiAccountsIndexOutOfBounds(_) => 14031,
121            LightSdkTypesError::InvalidCpiContextAccount => 14032,
122            LightSdkTypesError::InvalidSolPoolPdaAccount => 14033,
123            LightSdkTypesError::InvalidCpiAccountsOffset => 14034,
124            LightSdkTypesError::AccountError(e) => e.into(),
125            LightSdkTypesError::Hasher(e) => e.into(),
126            LightSdkTypesError::ConstraintViolation => 14035,
127            LightSdkTypesError::Borsh => 14036,
128            LightSdkTypesError::MissingCompressionInfo => 14037,
129            LightSdkTypesError::InvalidRentSponsor => 14038,
130            #[cfg(feature = "alloc")]
131            LightSdkTypesError::BorshIo(_) => 14039,
132            LightSdkTypesError::ReadOnlyAccountsNotSupportedInCpiContext => 14040,
133            LightSdkTypesError::CompressedAccountError(_) => 14041,
134            LightSdkTypesError::AccountDataTooSmall => 14042,
135            LightSdkTypesError::InvalidInstructionData => 14043,
136            LightSdkTypesError::InvalidSeeds => 14044,
137            LightSdkTypesError::CpiFailed => 14045,
138            LightSdkTypesError::NotEnoughAccountKeys => 14046,
139            LightSdkTypesError::MissingRequiredSignature => 14047,
140            LightSdkTypesError::ProgramError(code) => code,
141        }
142    }
143}