ethrpc_rs/error.rs
1//! Error types for the crate.
2
3use crate::jsonrpc::ErrorObject;
4
5/// The crate's result alias.
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Errors returned by RPC operations.
9#[derive(Debug, thiserror::Error)]
10pub enum Error {
11 /// No servers were provided or none were reachable. Returned by
12 /// [`evaluate`](crate::evaluate) and [`RpcList`](crate::RpcList).
13 #[error("no available server")]
14 NoAvailableServer,
15
16 /// The method was not found locally and no host is configured (the RPC was
17 /// created with an empty host and only handles overrides).
18 #[error("method not found")]
19 NotFound,
20
21 /// A JSON-RPC error object returned by the server. This is a *valid*
22 /// response — it is not retried against other servers by
23 /// [`RpcList`](crate::RpcList).
24 #[error("{0}")]
25 Rpc(ErrorObject),
26
27 /// A non-2xx HTTP status was returned with a body that was not a JSON-RPC
28 /// error. `body` is a trimmed snippet (at most 200 bytes).
29 #[error("HTTP {status} during {method}: {body}")]
30 Http {
31 /// The HTTP status code.
32 status: u16,
33 /// The RPC method that was being called.
34 method: String,
35 /// A trimmed snippet of the response body.
36 body: String,
37 },
38
39 /// A transport-level failure from the underlying HTTP client.
40 #[error("transport error: {0}")]
41 Transport(#[from] rsurl::Error),
42
43 /// A JSON encoding or decoding failure.
44 #[error("json error: {0}")]
45 Json(#[from] serde_json::Error),
46
47 /// Any other error (e.g. an override function failure, or an unsupported
48 /// argument shape).
49 #[error("{0}")]
50 Other(String),
51}
52
53impl Error {
54 /// Reports whether this error is a JSON-RPC error object (a valid response
55 /// from the server). Mirrors Go's `errors.As(err, &*ErrorObject)`.
56 pub fn is_rpc_error(&self) -> bool {
57 matches!(self, Error::Rpc(_))
58 }
59
60 /// Returns the underlying [`ErrorObject`] if this is a JSON-RPC error.
61 pub fn as_rpc_error(&self) -> Option<&ErrorObject> {
62 match self {
63 Error::Rpc(eo) => Some(eo),
64 _ => None,
65 }
66 }
67}