gmsol_solana_utils/
error.rs

1use solana_sdk::pubkey::Pubkey;
2
3/// Error type.
4#[derive(Debug, thiserror::Error)]
5pub enum Error {
6    /// Account Not Found.
7    #[error("account not found: {0}")]
8    AccountNotFound(Pubkey),
9    /// Parse url error.
10    #[error("parse url: {0}")]
11    ParseUrl(#[from] url::ParseError),
12    /// Parse cluster error.
13    #[error("parse cluster: {0}")]
14    ParseCluster(&'static str),
15    /// Merge transaction error.
16    #[error("merge transaction: {0}")]
17    MergeTransaction(&'static str),
18    /// Add transaction error.
19    #[error("add transaction: {0}")]
20    AddTransaction(&'static str),
21    /// Compile message error.
22    #[error("compile message: {0}")]
23    CompileMessage(#[from] solana_sdk::message::CompileError),
24    /// Client error.
25    #[cfg(feature = "solana-client")]
26    #[error("client: {0}")]
27    Client(#[from] Box<solana_client::client_error::ClientError>),
28    /// Signer error.
29    #[error("signer: {0}")]
30    Signer(#[from] solana_sdk::signer::SignerError),
31    /// Custom error.
32    #[error("custom: {0}")]
33    Custom(String),
34    /// RPC client error.
35    #[cfg(feature = "solana-rpc-client-api")]
36    #[error("rpc-client-api: {0}")]
37    RpcClientApi(Box<solana_rpc_client_api::client_error::Error>),
38    /// JSON error.
39    #[cfg(feature = "serde_json")]
40    #[error("json: {0}")]
41    Json(#[from] serde_json::Error),
42    /// Anchor error.
43    #[cfg(feature = "anchor-lang")]
44    #[error("anchor: {0}")]
45    Anchor(#[from] anchor_lang::error::Error),
46    /// Reqwest error.
47    #[cfg(feature = "reqwest")]
48    #[error("reqwest: {0}")]
49    Reqwest(#[from] reqwest::Error),
50}
51
52impl<T> From<(T, Error)> for Error {
53    fn from(value: (T, crate::Error)) -> Self {
54        value.1
55    }
56}
57
58impl Error {
59    /// Create a custom error.
60    pub fn custom(msg: impl ToString) -> Self {
61        Self::Custom(msg.to_string())
62    }
63}
64
65#[cfg(feature = "solana-rpc-client-api")]
66impl From<solana_rpc_client_api::client_error::Error> for Error {
67    fn from(err: solana_rpc_client_api::client_error::Error) -> Self {
68        Self::RpcClientApi(Box::new(err))
69    }
70}