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(Debug, Display, From)]
12pub enum Error {
13 #[display(fmt = "Server is unreachable")]
15 Unreachable,
16 #[display(fmt = "Decoder error: {}", _0)]
18 Decoder(String),
19 #[display(fmt = "Got invalid response: {}", _0)]
21 #[from(ignore)]
22 InvalidResponse(String),
23 #[display(fmt = "Transport error: {}", _0)]
25 #[from(ignore)]
26 Transport(String),
27 #[display(fmt = "RPC error: {:?}", _0)]
29 Rpc(RPCError),
30 #[display(fmt = "IO error: {}", _0)]
32 Io(IoError),
33 #[display(fmt = "Recovery error: {}", _0)]
35 Recovery(crate::signing::RecoveryError),
36 #[display(fmt = "Internal Web3 error")]
38 Internal,
39}
40
41impl std::error::Error for Error {
42 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
43 use self::Error::*;
44 match *self {
45 Unreachable | Decoder(_) | InvalidResponse(_) | Transport(_) | Internal => None,
46 Rpc(ref e) => Some(e),
47 Io(ref e) => Some(e),
48 Recovery(ref e) => Some(e),
49 }
50 }
51}
52
53impl From<SerdeError> for Error {
54 fn from(err: SerdeError) -> Self {
55 Error::Decoder(format!("{:?}", err))
56 }
57}
58
59impl Clone for Error {
60 fn clone(&self) -> Self {
61 use self::Error::*;
62 match self {
63 Unreachable => Unreachable,
64 Decoder(s) => Decoder(s.clone()),
65 InvalidResponse(s) => InvalidResponse(s.clone()),
66 Transport(s) => Transport(s.clone()),
67 Rpc(e) => Rpc(e.clone()),
68 Io(e) => Io(IoError::from(e.kind())),
69 Recovery(e) => Recovery(e.clone()),
70 Internal => Internal,
71 }
72 }
73}
74
75#[cfg(test)]
76impl PartialEq for Error {
77 fn eq(&self, other: &Self) -> bool {
78 use self::Error::*;
79 match (self, other) {
80 (Unreachable, Unreachable) | (Internal, Internal) => true,
81 (Decoder(a), Decoder(b)) | (InvalidResponse(a), InvalidResponse(b)) | (Transport(a), Transport(b)) => {
82 a == b
83 }
84 (Rpc(a), Rpc(b)) => a == b,
85 (Io(a), Io(b)) => a.kind() == b.kind(),
86 (Recovery(a), Recovery(b)) => a == b,
87 _ => false,
88 }
89 }
90}