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(
50        "Indexer not initialized. Set photon_url in LightClientConfig to enable indexer API calls."
51    )]
52    NotInitialized,
53    #[error("Indexer slot has not reached the requested slot.")]
54    IndexerNotSyncedToSlot,
55    #[error("Address Merkle trees cannot be packed as output Merkle trees.")]
56    InvalidPackTreeType,
57}
58
59impl IndexerError {
60    pub fn missing_result(context: impl Into<String>, message: impl Into<String>) -> Self {
61        Self::MissingResult {
62            context: context.into(),
63            message: message.into(),
64        }
65    }
66
67    pub fn api_error(error: impl std::fmt::Display) -> Self {
68        Self::ApiError(error.to_string())
69    }
70
71    pub fn decode_error(field: impl Into<String>, error: impl std::fmt::Display) -> Self {
72        Self::DataDecodeError {
73            field: field.into(),
74            message: error.to_string(),
75        }
76    }
77
78    pub fn base58_decode_error(field: impl Into<String>, error: impl std::fmt::Display) -> Self {
79        Self::Base58DecodeError {
80            field: field.into(),
81            message: error.to_string(),
82        }
83    }
84}
85
86impl<T> From<photon_api::apis::Error<T>> for IndexerError {
87    fn from(error: photon_api::apis::Error<T>) -> Self {
88        match error {
89            photon_api::apis::Error::Reqwest(e) => {
90                IndexerError::ApiError(format!("Request error: {}", e))
91            }
92            photon_api::apis::Error::Serde(e) => {
93                IndexerError::ApiError(format!("Serialization error: {}", e))
94            }
95            photon_api::apis::Error::Io(e) => IndexerError::ApiError(format!("IO error: {}", e)),
96            _ => IndexerError::ApiError(format!("Unknown API error {}", error)),
97        }
98    }
99}
100
101impl Clone for IndexerError {
102    fn clone(&self) -> Self {
103        match self {
104            IndexerError::PhotonError { context, message } => IndexerError::PhotonError {
105                context: context.clone(),
106                message: message.clone(),
107            },
108            IndexerError::RpcError(message) => IndexerError::RpcError(message.clone()),
109            IndexerError::DeserializeError(err) => IndexerError::DeserializeError(err.clone()),
110            IndexerError::ApiError(message) => IndexerError::ApiError(message.clone()),
111            IndexerError::MissingResult { context, message } => IndexerError::MissingResult {
112                context: context.clone(),
113                message: message.clone(),
114            },
115            IndexerError::AccountNotFound => IndexerError::AccountNotFound,
116            IndexerError::Base58DecodeError { field, message } => IndexerError::Base58DecodeError {
117                field: field.clone(),
118                message: message.clone(),
119            },
120            IndexerError::InvalidParameters(message) => {
121                IndexerError::InvalidParameters(message.clone())
122            }
123            IndexerError::DataDecodeError { field, message } => IndexerError::DataDecodeError {
124                field: field.clone(),
125                message: message.clone(),
126            },
127            IndexerError::NotImplemented(message) => IndexerError::NotImplemented(message.clone()),
128            IndexerError::Unknown(message) => IndexerError::Unknown(message.clone()),
129            IndexerError::ReferenceIndexedMerkleTreeError(_) => {
130                IndexerError::CustomError("ReferenceIndexedMerkleTreeError".to_string())
131            }
132            IndexerError::IndexedMerkleTreeError(_) => {
133                IndexerError::CustomError("IndexedMerkleTreeError".to_string())
134            }
135            IndexerError::InvalidResponseData => IndexerError::InvalidResponseData,
136            IndexerError::CustomError(_) => IndexerError::CustomError("IndexerError".to_string()),
137            IndexerError::NotInitialized => IndexerError::NotInitialized,
138            IndexerError::IndexerNotSyncedToSlot => IndexerError::IndexerNotSyncedToSlot,
139            IndexerError::InvalidPackTreeType => IndexerError::InvalidPackTreeType,
140        }
141    }
142}