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 From<crate::rpc::RpcError> for IndexerError {
102    fn from(error: crate::rpc::RpcError) -> Self {
103        IndexerError::RpcError(error.to_string())
104    }
105}
106
107impl Clone for IndexerError {
108    fn clone(&self) -> Self {
109        match self {
110            IndexerError::PhotonError { context, message } => IndexerError::PhotonError {
111                context: context.clone(),
112                message: message.clone(),
113            },
114            IndexerError::RpcError(message) => IndexerError::RpcError(message.clone()),
115            IndexerError::DeserializeError(err) => IndexerError::DeserializeError(err.clone()),
116            IndexerError::ApiError(message) => IndexerError::ApiError(message.clone()),
117            IndexerError::MissingResult { context, message } => IndexerError::MissingResult {
118                context: context.clone(),
119                message: message.clone(),
120            },
121            IndexerError::AccountNotFound => IndexerError::AccountNotFound,
122            IndexerError::Base58DecodeError { field, message } => IndexerError::Base58DecodeError {
123                field: field.clone(),
124                message: message.clone(),
125            },
126            IndexerError::InvalidParameters(message) => {
127                IndexerError::InvalidParameters(message.clone())
128            }
129            IndexerError::DataDecodeError { field, message } => IndexerError::DataDecodeError {
130                field: field.clone(),
131                message: message.clone(),
132            },
133            IndexerError::NotImplemented(message) => IndexerError::NotImplemented(message.clone()),
134            IndexerError::Unknown(message) => IndexerError::Unknown(message.clone()),
135            IndexerError::ReferenceIndexedMerkleTreeError(_) => {
136                IndexerError::CustomError("ReferenceIndexedMerkleTreeError".to_string())
137            }
138            IndexerError::IndexedMerkleTreeError(_) => {
139                IndexerError::CustomError("IndexedMerkleTreeError".to_string())
140            }
141            IndexerError::InvalidResponseData => IndexerError::InvalidResponseData,
142            IndexerError::CustomError(_) => IndexerError::CustomError("IndexerError".to_string()),
143            IndexerError::NotInitialized => IndexerError::NotInitialized,
144            IndexerError::IndexerNotSyncedToSlot => IndexerError::IndexerNotSyncedToSlot,
145            IndexerError::InvalidPackTreeType => IndexerError::InvalidPackTreeType,
146        }
147    }
148}