poseidon_client/
errors.rs

1use borsh::{BorshDeserialize, BorshSerialize};
2use core::fmt;
3use serde::{Deserialize, Serialize};
4
5pub type PoseidonResult<T> = Result<T, PoseidonError>;
6
7#[derive(Debug)]
8pub enum PoseidonError {
9    Io(std::io::ErrorKind),
10    /// Errors from the minreq crate
11    Http(Minreq),
12    /// Errors from the bincode crate
13    BincodeError(bincode::ErrorKind),
14    /// Errors encountered when using serde_json crate to deserialize
15    SerdeJsonDeser(String),
16    /// The maximum length of the `seed` provided has been exceeded
17    /// as indicated by `[MAX_SEED_LEN]`
18    MaxSeedLengthExceeded,
19    /// The `owner` public key of the PDA provided is the same as the
20    /// `[PDA_MARKER]` address. This is not allowed.
21    IllegalOwner,
22    /// The error that occured hasn't been encountered before
23    UnspecifiedError,
24    /// The string provided is not valid for the Base58 format.
25    InvalidBase58ForPublicKey,
26    /// Unable to convert a `slice` to an array of 32 bytes (`[u8; 32]`).
27    ErrorConvertingToU832,
28    /// The program ID was not found in the provided instruction
29    ProgramIdNotFound,
30    /// The public key was not found in the accounts found in the `Message`
31    PublicKeyNotFoundInMessageAccounts,
32    /// The account index was not found in the `Accounts`
33    AccountIndexNotFoundInMessageAccounts,
34    /// Error decoding string as Base58 format
35    Bs58Decode(bs58::decode::Error),
36    /// Error encoding to base58 format
37    Bs58Encode(bs58::encode::Error),
38    /// The transaction was not found in the Cluster
39    TransactionNotFoundInCluster,
40}
41
42impl std::error::Error for PoseidonError {}
43
44impl fmt::Display for PoseidonError {
45    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46        write!(f, "{:?}", self)
47    }
48}
49
50impl From<bs58::encode::Error> for PoseidonError {
51    fn from(error: bs58::encode::Error) -> Self {
52        PoseidonError::Bs58Encode(error)
53    }
54}
55
56impl From<bs58::decode::Error> for PoseidonError {
57    fn from(error: bs58::decode::Error) -> Self {
58        PoseidonError::Bs58Decode(error)
59    }
60}
61
62impl From<bincode::Error> for PoseidonError {
63    fn from(error: bincode::Error) -> Self {
64        PoseidonError::BincodeError(*error)
65    }
66}
67
68impl From<std::io::Error> for PoseidonError {
69    fn from(io_error: std::io::Error) -> Self {
70        PoseidonError::Io(io_error.kind())
71    }
72}
73
74impl From<serde_json::Error> for PoseidonError {
75    fn from(error: serde_json::Error) -> Self {
76        PoseidonError::SerdeJsonDeser(error.to_string())
77    }
78}
79
80impl From<minreq::Error> for PoseidonError {
81    fn from(minreq_error: minreq::Error) -> Self {
82        PoseidonError::Http(match minreq_error {
83            minreq::Error::InvalidUtf8InBody(utf8_error) => Minreq::InvalidUtf8InBody(utf8_error),
84            minreq::Error::RustlsCreateConnection(rustls_error) => {
85                Minreq::RustlsCreateConnection(rustls_error.to_string())
86            }
87            minreq::Error::IoError(io_error) => Minreq::Io(io_error.kind()),
88            minreq::Error::MalformedChunkLength => Minreq::MalformedChunkLength,
89            minreq::Error::MalformedChunkEnd => Minreq::MalformedChunkEnd,
90            minreq::Error::MalformedContentLength => Minreq::MalformedContentLength,
91            minreq::Error::HeadersOverflow => Minreq::HeadersOverflow,
92            minreq::Error::StatusLineOverflow => Minreq::StatusLineOverflow,
93            minreq::Error::AddressNotFound => Minreq::AddressNotFound,
94            minreq::Error::RedirectLocationMissing => Minreq::RedirectLocationMissing,
95            minreq::Error::InfiniteRedirectionLoop => Minreq::InfiniteRedirectionLoop,
96            minreq::Error::TooManyRedirections => Minreq::TooManyRedirections,
97            minreq::Error::InvalidUtf8InResponse => Minreq::InvalidUtf8InResponse,
98            minreq::Error::PunycodeConversionFailed => Minreq::PunycodeConversionFailed,
99            minreq::Error::HttpsFeatureNotEnabled => Minreq::HttpsFeatureNotEnabled,
100            minreq::Error::PunycodeFeatureNotEnabled => Minreq::PunycodeFeatureNotEnabled,
101            minreq::Error::BadProxy => Minreq::BadProxy,
102            minreq::Error::BadProxyCreds => Minreq::BadProxyCreds,
103            minreq::Error::ProxyConnect => Minreq::ProxyConnect,
104            minreq::Error::InvalidProxyCreds => Minreq::InvalidProxyCreds,
105            minreq::Error::Other(other_error) => Minreq::Other(other_error),
106        })
107    }
108}
109
110/// Errors from the minreq crate
111/// Manual implementation provides Comparison and Clone operations
112#[derive(Debug, Eq, PartialEq, Clone)]
113pub enum Minreq {
114    /// The response body contains invalid UTF-8, so the `as_str()`
115    /// conversion failed.
116    InvalidUtf8InBody(core::str::Utf8Error),
117    /// Ran into a rustls error while creating the connection.
118    RustlsCreateConnection(String),
119    /// Ran into an IO problem while loading the response.
120    Io(std::io::ErrorKind),
121    /// Couldn't parse the incoming chunk's length while receiving a
122    /// response with the header `Transfer-Encoding: chunked`.
123    MalformedChunkLength,
124    /// The chunk did not end after reading the previously read amount
125    /// of bytes.
126    MalformedChunkEnd,
127    /// Couldn't parse the `Content-Length` header's value as an
128    /// `usize`.
129    MalformedContentLength,
130    /// The response contains headers whose total size surpasses
131    HeadersOverflow,
132    /// The response's status line length surpasses
133    StatusLineOverflow,
134    /// [ToSocketAddrs](std::net::ToSocketAddrs) did not resolve to an
135    /// address.
136    AddressNotFound,
137    /// The response was a redirection, but the `Location` header is
138    /// missing.
139    RedirectLocationMissing,
140    /// The response redirections caused an infinite redirection loop.
141    InfiniteRedirectionLoop,
142    /// Redirections, won't follow any more.
143    TooManyRedirections,
144    /// The response contained invalid UTF-8 where it should be valid
145    /// (eg. headers), so the response cannot interpreted correctly.
146    InvalidUtf8InResponse,
147    /// The provided url contained a domain that has non-ASCII
148    /// characters, and could not be converted into punycode. It is
149    /// probably not an actual domain.
150    PunycodeConversionFailed,
151    /// Tried to send a secure request (ie. the url started with
152    /// `https://`), but the crate's `https` feature was not enabled,
153    /// and as such, a connection cannot be made.
154    HttpsFeatureNotEnabled,
155    /// The provided url contained a domain that has non-ASCII
156    /// characters, but it could not be converted into punycode
157    /// because the `punycode` feature was not enabled.
158    PunycodeFeatureNotEnabled,
159    /// The provided proxy information was not properly formatted.
160    /// Supported proxy format is `[user:password@]host:port`.
161    BadProxy,
162    /// The provided credentials were rejected by the proxy server.
163    BadProxyCreds,
164    /// The provided proxy credentials were malformed.
165    ProxyConnect,
166    /// The provided credentials were rejected by the proxy server.
167    InvalidProxyCreds,
168
169    /// This is a special error case, one that should never be
170    /// returned! Think of this as a cleaner alternative to calling
171    /// `unreachable!()` inside the library. If you come across this,
172    /// please open an issue in the minreq crate repository, and include the string inside this
173    /// error, as it can be used to locate the problem.
174    Other(&'static str),
175}
176
177#[derive(
178    Debug, Serialize, Deserialize, BorshSerialize, BorshDeserialize, PartialEq, PartialOrd, Clone,
179)]
180pub struct RpcResponseJsonError {
181    jsonrpc: String,
182    error: JsonRpcError,
183    id: u8,
184}
185
186#[derive(
187    Debug, Serialize, Deserialize, BorshSerialize, BorshDeserialize, PartialEq, PartialOrd, Clone,
188)]
189pub struct JsonRpcError {
190    code: i16,
191    message: String,
192}