light_sdk/
error.rs

1use light_hasher::HasherError;
2use light_sdk_types::error::LightSdkTypesError;
3use light_zero_copy::errors::ZeroCopyError;
4use thiserror::Error;
5
6use crate::ProgramError;
7
8pub type Result<T> = std::result::Result<T, LightSdkError>;
9
10#[derive(Debug, Error, PartialEq)]
11pub enum LightSdkError {
12    #[error("Constraint violation")]
13    ConstraintViolation,
14    #[error("Invalid light-system-program ID")]
15    InvalidLightSystemProgram,
16    #[error("Expected accounts in the instruction")]
17    ExpectedAccounts,
18    #[error("Expected address Merkle context to be provided")]
19    ExpectedAddressTreeInfo,
20    #[error("Expected address root index to be provided")]
21    ExpectedAddressRootIndex,
22    #[error("Accounts with a specified input are expected to have data")]
23    ExpectedData,
24    #[error("Accounts with specified data are expected to have a discriminator")]
25    ExpectedDiscriminator,
26    #[error("Accounts with specified data are expected to have a hash")]
27    ExpectedHash,
28    #[error("Expected the `{0}` light account to be provided")]
29    ExpectedLightSystemAccount(String),
30    #[error("`mut` and `close` accounts are expected to have a Merkle context")]
31    ExpectedMerkleContext,
32    #[error("Expected root index to be provided")]
33    ExpectedRootIndex,
34    #[error("Cannot transfer lamports from an account without input")]
35    TransferFromNoInput,
36    #[error("Cannot transfer from an account without lamports")]
37    TransferFromNoLamports,
38    #[error("Account, from which a transfer was attempted, has insufficient amount of lamports")]
39    TransferFromInsufficientLamports,
40    #[error("Integer overflow resulting from too large resulting amount")]
41    TransferIntegerOverflow,
42    #[error("Borsh error.")]
43    Borsh,
44    #[error("Fewer accounts than number of system accounts.")]
45    FewerAccountsThanSystemAccounts,
46    #[error("InvalidCpiSignerAccount")]
47    InvalidCpiSignerAccount,
48    #[error("Missing meta field: {0}")]
49    MissingField(String),
50    #[error("Output state tree index is none. Use an CompressedAccountMeta type with output tree index to initialize or update accounts.")]
51    OutputStateTreeIndexIsNone,
52    #[error("Address is none during initialization")]
53    InitAddressIsNone,
54    #[error("Address is none during initialization with address")]
55    InitWithAddressIsNone,
56    #[error("Output is none during initialization with address")]
57    InitWithAddressOutputIsNone,
58    #[error("Address is none during meta mutation")]
59    MetaMutAddressIsNone,
60    #[error("Input is none during meta mutation")]
61    MetaMutInputIsNone,
62    #[error("Output lamports is none during meta mutation")]
63    MetaMutOutputLamportsIsNone,
64    #[error("Output is none during meta mutation")]
65    MetaMutOutputIsNone,
66    #[error("Address is none during meta close")]
67    MetaCloseAddressIsNone,
68    #[error("Input is none during meta close")]
69    MetaCloseInputIsNone,
70    #[error("CPI accounts index out of bounds: {0}")]
71    CpiAccountsIndexOutOfBounds(usize),
72    #[error(transparent)]
73    Hasher(#[from] HasherError),
74    #[error(transparent)]
75    ZeroCopy(#[from] ZeroCopyError),
76    #[error("Program error: {0}")]
77    ProgramError(#[from] ProgramError),
78}
79
80impl From<LightSdkError> for ProgramError {
81    fn from(e: LightSdkError) -> Self {
82        ProgramError::Custom(e.into())
83    }
84}
85
86impl From<LightSdkTypesError> for LightSdkError {
87    fn from(e: LightSdkTypesError) -> Self {
88        match e {
89            LightSdkTypesError::InitAddressIsNone => LightSdkError::InitAddressIsNone,
90            LightSdkTypesError::InitWithAddressIsNone => LightSdkError::InitWithAddressIsNone,
91            LightSdkTypesError::InitWithAddressOutputIsNone => {
92                LightSdkError::InitWithAddressOutputIsNone
93            }
94            LightSdkTypesError::MetaMutAddressIsNone => LightSdkError::MetaMutAddressIsNone,
95            LightSdkTypesError::MetaMutInputIsNone => LightSdkError::MetaMutInputIsNone,
96            LightSdkTypesError::MetaMutOutputLamportsIsNone => {
97                LightSdkError::MetaMutOutputLamportsIsNone
98            }
99            LightSdkTypesError::MetaMutOutputIsNone => LightSdkError::MetaMutOutputIsNone,
100            LightSdkTypesError::MetaCloseAddressIsNone => LightSdkError::MetaCloseAddressIsNone,
101            LightSdkTypesError::MetaCloseInputIsNone => LightSdkError::MetaCloseInputIsNone,
102            LightSdkTypesError::FewerAccountsThanSystemAccounts => {
103                LightSdkError::FewerAccountsThanSystemAccounts
104            }
105            LightSdkTypesError::CpiAccountsIndexOutOfBounds(index) => {
106                LightSdkError::CpiAccountsIndexOutOfBounds(index)
107            }
108            LightSdkTypesError::Hasher(e) => LightSdkError::Hasher(e),
109        }
110    }
111}
112
113impl From<LightSdkError> for u32 {
114    fn from(e: LightSdkError) -> Self {
115        match e {
116            LightSdkError::ConstraintViolation => 16001,
117            LightSdkError::InvalidLightSystemProgram => 16002,
118            LightSdkError::ExpectedAccounts => 16003,
119            LightSdkError::ExpectedAddressTreeInfo => 16004,
120            LightSdkError::ExpectedAddressRootIndex => 16005,
121            LightSdkError::ExpectedData => 16006,
122            LightSdkError::ExpectedDiscriminator => 16007,
123            LightSdkError::ExpectedHash => 16008,
124            LightSdkError::ExpectedLightSystemAccount(_) => 16009,
125            LightSdkError::ExpectedMerkleContext => 16010,
126            LightSdkError::ExpectedRootIndex => 16011,
127            LightSdkError::TransferFromNoInput => 16012,
128            LightSdkError::TransferFromNoLamports => 16013,
129            LightSdkError::TransferFromInsufficientLamports => 16014,
130            LightSdkError::TransferIntegerOverflow => 16015,
131            LightSdkError::Borsh => 16016,
132            LightSdkError::FewerAccountsThanSystemAccounts => 16017,
133            LightSdkError::InvalidCpiSignerAccount => 16018,
134            LightSdkError::MissingField(_) => 16019,
135            LightSdkError::OutputStateTreeIndexIsNone => 16020,
136            LightSdkError::InitAddressIsNone => 16021,
137            LightSdkError::InitWithAddressIsNone => 16022,
138            LightSdkError::InitWithAddressOutputIsNone => 16023,
139            LightSdkError::MetaMutAddressIsNone => 16024,
140            LightSdkError::MetaMutInputIsNone => 16025,
141            LightSdkError::MetaMutOutputLamportsIsNone => 16026,
142            LightSdkError::MetaMutOutputIsNone => 16027,
143            LightSdkError::MetaCloseAddressIsNone => 16028,
144            LightSdkError::MetaCloseInputIsNone => 16029,
145            LightSdkError::CpiAccountsIndexOutOfBounds(_) => 16031,
146            LightSdkError::Hasher(e) => e.into(),
147            LightSdkError::ZeroCopy(e) => e.into(),
148            LightSdkError::ProgramError(e) => u64::from(e) as u32,
149        }
150    }
151}