light_client/indexer/
error.rs

1use thiserror::Error;
2
3#[derive(Error, Debug, PartialEq)]
4pub enum IndexerError {
5    #[error("Photon API error in {context}: {message}")]
6    PhotonError { context: String, message: String },
7
8    #[error("RPC error: {0}")]
9    RpcError(String),
10
11    #[error("Failed to deserialize account data: {0}")]
12    DeserializeError(#[from] solana_program_error::ProgramError),
13
14    #[error("API error: {0}")]
15    ApiError(String),
16
17    #[error("Missing result from {context}: {message}")]
18    MissingResult { context: String, message: String },
19
20    #[error("Account not found")]
21    AccountNotFound,
22
23    #[error("Base58 decode error: {field} - {message}")]
24    Base58DecodeError { field: String, message: String },
25
26    #[error("Invalid parameters: {0}")]
27    InvalidParameters(String),
28
29    #[error("Data decode error: {field} - {message}")]
30    DataDecodeError { field: String, message: String },
31
32    #[error("Method not implemented: {0}")]
33    NotImplemented(String),
34
35    #[error("Unknown error: {0}")]
36    Unknown(String),
37
38    #[error("Indexed Merkle tree reference v1 error: {0}")]
39    ReferenceIndexedMerkleTreeError(
40        #[from] light_indexed_merkle_tree::reference::IndexedReferenceMerkleTreeError,
41    ),
42    #[error("Indexed Merkle tree v1 error: {0}")]
43    IndexedMerkleTreeError(#[from] light_indexed_merkle_tree::errors::IndexedMerkleTreeError),
44    #[error("Invalid response data")]
45    InvalidResponseData,
46
47    #[error("Error: `{0}`")]
48    CustomError(String),
49    #[error("Indexer not initialized.")]
50    NotInitialized,
51    #[error("Indexer slot has not reached the requested slot.")]
52    IndexerNotSyncedToSlot,
53    #[error("Address Merkle trees cannot be packed as output Merkle trees.")]
54    InvalidPackTreeType,
55}
56
57impl IndexerError {
58    pub fn missing_result(context: impl Into<String>, message: impl Into<String>) -> Self {
59        Self::MissingResult {
60            context: context.into(),
61            message: message.into(),
62        }
63    }
64
65    pub fn api_error(error: impl std::fmt::Display) -> Self {
66        Self::ApiError(error.to_string())
67    }
68
69    pub fn decode_error(field: impl Into<String>, error: impl std::fmt::Display) -> Self {
70        Self::DataDecodeError {
71            field: field.into(),
72            message: error.to_string(),
73        }
74    }
75
76    pub fn base58_decode_error(field: impl Into<String>, error: impl std::fmt::Display) -> Self {
77        Self::Base58DecodeError {
78            field: field.into(),
79            message: error.to_string(),
80        }
81    }
82}
83
84impl<T> From<photon_api::apis::Error<T>> for IndexerError {
85    fn from(error: photon_api::apis::Error<T>) -> Self {
86        match error {
87            photon_api::apis::Error::Reqwest(e) => {
88                IndexerError::ApiError(format!("Request error: {}", e))
89            }
90            photon_api::apis::Error::Serde(e) => {
91                IndexerError::ApiError(format!("Serialization error: {}", e))
92            }
93            photon_api::apis::Error::Io(e) => IndexerError::ApiError(format!("IO error: {}", e)),
94            _ => IndexerError::ApiError("Unknown API error".to_string()),
95        }
96    }
97}
98
99impl Clone for IndexerError {
100    fn clone(&self) -> Self {
101        match self {
102            IndexerError::PhotonError { context, message } => IndexerError::PhotonError {
103                context: context.clone(),
104                message: message.clone(),
105            },
106            IndexerError::RpcError(message) => IndexerError::RpcError(message.clone()),
107            IndexerError::DeserializeError(err) => IndexerError::DeserializeError(err.clone()),
108            IndexerError::ApiError(message) => IndexerError::ApiError(message.clone()),
109            IndexerError::MissingResult { context, message } => IndexerError::MissingResult {
110                context: context.clone(),
111                message: message.clone(),
112            },
113            IndexerError::AccountNotFound => IndexerError::AccountNotFound,
114            IndexerError::Base58DecodeError { field, message } => IndexerError::Base58DecodeError {
115                field: field.clone(),
116                message: message.clone(),
117            },
118            IndexerError::InvalidParameters(message) => {
119                IndexerError::InvalidParameters(message.clone())
120            }
121            IndexerError::DataDecodeError { field, message } => IndexerError::DataDecodeError {
122                field: field.clone(),
123                message: message.clone(),
124            },
125            IndexerError::NotImplemented(message) => IndexerError::NotImplemented(message.clone()),
126            IndexerError::Unknown(message) => IndexerError::Unknown(message.clone()),
127            IndexerError::ReferenceIndexedMerkleTreeError(_) => {
128                IndexerError::CustomError("ReferenceIndexedMerkleTreeError".to_string())
129            }
130            IndexerError::IndexedMerkleTreeError(_) => {
131                IndexerError::CustomError("IndexedMerkleTreeError".to_string())
132            }
133            IndexerError::InvalidResponseData => IndexerError::InvalidResponseData,
134            IndexerError::CustomError(_) => IndexerError::CustomError("IndexerError".to_string()),
135            IndexerError::NotInitialized => IndexerError::NotInitialized,
136            IndexerError::IndexerNotSyncedToSlot => IndexerError::IndexerNotSyncedToSlot,
137            IndexerError::InvalidPackTreeType => IndexerError::InvalidPackTreeType,
138        }
139    }
140}