1use crate::rpc::error::Error as RPCError;
3use derive_more::{Display, From};
4use serde_json::Error as SerdeError;
5use std::io::Error as IoError;
6
7pub type Result<T = ()> = std::result::Result<T, Error>;
9
10#[derive(Display, Debug, Clone, PartialEq)]
12pub enum TransportError {
13 #[display(fmt = "code {}", _0)]
15 Code(u16),
16 #[display(fmt = "{}", _0)]
18 Message(String),
19}
20
21#[derive(Debug, Display, From)]
23pub enum Error {
24 #[display(fmt = "Server is unreachable")]
26 Unreachable,
27 #[display(fmt = "Decoder error: {}", _0)]
29 Decoder(String),
30 #[display(fmt = "Got invalid response: {}", _0)]
32 #[from(ignore)]
33 InvalidResponse(String),
34 #[display(fmt = "Transport error: {}" _0)]
36 #[from(ignore)]
37 Transport(TransportError),
38 #[display(fmt = "RPC error: {:?}", _0)]
40 Rpc(RPCError),
41 #[display(fmt = "IO error: {}", _0)]
43 Io(IoError),
44 #[display(fmt = "Recovery error: {}", _0)]
46 Recovery(crate::signing::RecoveryError),
47 #[display(fmt = "Internal Web3 error")]
49 Internal,
50}
51
52impl std::error::Error for Error {
53 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
54 use self::Error::*;
55 match *self {
56 Unreachable | Decoder(_) | InvalidResponse(_) | Transport { .. } | Internal => None,
57 Rpc(ref e) => Some(e),
58 Io(ref e) => Some(e),
59 Recovery(ref e) => Some(e),
60 }
61 }
62}
63
64impl From<SerdeError> for Error {
65 fn from(err: SerdeError) -> Self {
66 Error::Decoder(format!("{:?}", err))
67 }
68}
69
70impl Clone for Error {
71 fn clone(&self) -> Self {
72 use self::Error::*;
73 match self {
74 Unreachable => Unreachable,
75 Decoder(s) => Decoder(s.clone()),
76 InvalidResponse(s) => InvalidResponse(s.clone()),
77 Transport(s) => Transport(s.clone()),
78 Rpc(e) => Rpc(e.clone()),
79 Io(e) => Io(IoError::from(e.kind())),
80 Recovery(e) => Recovery(e.clone()),
81 Internal => Internal,
82 }
83 }
84}
85
86#[cfg(test)]
87impl PartialEq for Error {
88 fn eq(&self, other: &Self) -> bool {
89 use self::Error::*;
90 match (self, other) {
91 (Unreachable, Unreachable) | (Internal, Internal) => true,
92 (Decoder(a), Decoder(b)) | (InvalidResponse(a), InvalidResponse(b)) => a == b,
93 (Transport(a), Transport(b)) => a == b,
94 (Rpc(a), Rpc(b)) => a == b,
95 (Io(a), Io(b)) => a.kind() == b.kind(),
96 (Recovery(a), Recovery(b)) => a == b,
97 _ => false,
98 }
99 }
100}