spl_account_compression/
error.rs

1use anchor_lang::{
2    prelude::*,
3    solana_program::{msg, program_error::ProgramError},
4};
5use bytemuck::PodCastError;
6use spl_concurrent_merkle_tree::error::ConcurrentMerkleTreeError;
7use std::any::type_name;
8use std::mem::size_of;
9
10/// Errors related to misconfiguration or misuse of the Merkle tree
11#[error_code]
12pub enum AccountCompressionError {
13    /// This error is currently not used.
14    #[msg("Incorrect leaf length. Expected vec of 32 bytes")]
15    IncorrectLeafLength,
16
17    /// A modification to the tree was invalid and a changelog was not emitted.
18    /// The proof may be invalid or out-of-date, or the provided leaf hash was invalid.
19    #[msg("Concurrent merkle tree error")]
20    ConcurrentMerkleTreeError,
21
22    /// An issue was detected with loading the provided account data for this ConcurrentMerkleTree.
23    #[msg("Issue zero copying concurrent merkle tree data")]
24    ZeroCopyError,
25
26    /// See [ConcurrentMerkleTreeHeader](/spl_account_compression/state/struct.ConcurrentMerkleTreeHeader.html) for valid configuration options.
27    #[msg("An unsupported max depth or max buffer size constant was provided")]
28    ConcurrentMerkleTreeConstantsError,
29
30    /// When using Canopy, the stored byte length should a multiple of the node's byte length (32 bytes)
31    #[msg("Expected a different byte length for the merkle tree canopy")]
32    CanopyLengthMismatch,
33
34    /// Incorrect authority account provided
35    #[msg("Provided authority does not match expected tree authority")]
36    IncorrectAuthority,
37
38    /// Incorrect account owner
39    #[msg("Account is owned by a different program, expected it to be owned by this program")]
40    IncorrectAccountOwner,
41
42    /// Incorrect account type
43    #[msg("Account provided has incorrect account type")]
44    IncorrectAccountType,
45
46    /// Tree information cannot be processed because the provided leaf_index
47    /// is out of bounds of tree's maximum leaf capacity
48    #[msg("Leaf index of concurrent merkle tree is out of bounds")]
49    LeafIndexOutOfBounds,
50}
51
52impl From<&ConcurrentMerkleTreeError> for AccountCompressionError {
53    fn from(_error: &ConcurrentMerkleTreeError) -> Self {
54        AccountCompressionError::ConcurrentMerkleTreeError
55    }
56}
57
58pub fn error_msg<T>(data_len: usize) -> impl Fn(PodCastError) -> ProgramError {
59    move |_: PodCastError| -> ProgramError {
60        msg!(
61            "Failed to load {}. Size is {}, expected {}",
62            type_name::<T>(),
63            data_len,
64            size_of::<T>(),
65        );
66        ProgramError::InvalidAccountData
67    }
68}