pink_web3/
error.rs

1//! Web3 Error
2use crate::prelude::*;
3use derive_more::{Display, From};
4use json::de::Error as SerdeError;
5#[cfg(feature = "std")]
6use std::io::Error as IoError;
7
8/// Web3 `Result` type.
9pub type Result<T = ()> = std::result::Result<T, Error>;
10
11/// Transport-depended error.
12#[derive(Display, Debug, Clone, PartialEq)]
13pub enum TransportError {
14    /// Transport-specific error code.
15    #[display(fmt = "code {}", _0)]
16    Code(u16),
17    /// Arbitrary, developer-readable description of the occurred error.
18    #[display(fmt = "{}", _0)]
19    Message(String),
20}
21
22/// Errors which can occur when attempting to generate resource uri.
23#[derive(Debug, Display, From)]
24pub enum Error {
25    /// server is unreachable
26    #[display(fmt = "Server is unreachable")]
27    Unreachable,
28    /// decoder error
29    #[display(fmt = "Decoder error: {}", _0)]
30    Decoder(String),
31    /// invalid response
32    #[display(fmt = "Got invalid response: {}", _0)]
33    #[from(ignore)]
34    InvalidResponse(String),
35    /// transport error
36    #[display(fmt = "Transport error: {}" _0)]
37    #[from(ignore)]
38    Transport(TransportError),
39    /// rpc error
40    #[display(fmt = "RPC error: {:?}", _0)]
41    #[from(ignore)]
42    Rpc(String),
43    /// io error
44    #[cfg(feature = "std")]
45    #[display(fmt = "IO error: {}", _0)]
46    Io(IoError),
47    /// recovery error
48    #[display(fmt = "Recovery error: {}", _0)]
49    Recovery(crate::signing::RecoveryError),
50    /// web3 internal error
51    #[display(fmt = "Internal Web3 error")]
52    Internal,
53}
54
55#[cfg(feature = "std")]
56impl std::error::Error for Error {
57    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
58        use self::Error::*;
59        match *self {
60            Unreachable | Decoder(_) | InvalidResponse(_) | Transport { .. } | Internal => None,
61            Rpc(_) => None,
62            Io(ref e) => Some(e),
63            Recovery(ref e) => Some(e),
64        }
65    }
66}
67
68impl From<SerdeError> for Error {
69    fn from(err: SerdeError) -> Self {
70        Error::Decoder(format!("{:?}", err))
71    }
72}
73
74impl Clone for Error {
75    fn clone(&self) -> Self {
76        use self::Error::*;
77        match self {
78            Unreachable => Unreachable,
79            Decoder(s) => Decoder(s.clone()),
80            InvalidResponse(s) => InvalidResponse(s.clone()),
81            Transport(s) => Transport(s.clone()),
82            Rpc(e) => Rpc(e.clone()),
83            #[cfg(feature = "std")]
84            Io(e) => Io(IoError::from(e.kind())),
85            Recovery(e) => Recovery(e.clone()),
86            Internal => Internal,
87        }
88    }
89}
90
91#[cfg(test)]
92impl PartialEq for Error {
93    fn eq(&self, other: &Self) -> bool {
94        use self::Error::*;
95        match (self, other) {
96            (Unreachable, Unreachable) | (Internal, Internal) => true,
97            (Decoder(a), Decoder(b)) | (InvalidResponse(a), InvalidResponse(b)) => a == b,
98            (Transport(a), Transport(b)) => a == b,
99            (Rpc(a), Rpc(b)) => a == b,
100            (Io(a), Io(b)) => a.kind() == b.kind(),
101            (Recovery(a), Recovery(b)) => a == b,
102            _ => false,
103        }
104    }
105}