ckb_network/
errors.rs

1//! Error module
2use p2p::{
3    SessionId,
4    error::{
5        DialerErrorKind, ListenErrorKind, ProtocolHandleErrorKind, SendErrorKind,
6        TransportErrorKind,
7    },
8    secio::PeerId,
9};
10use std::fmt;
11use std::fmt::Display;
12use std::io::Error as IoError;
13
14/// Alias result on network module
15pub type Result<T> = ::std::result::Result<T, Error>;
16
17/// All error on network module
18#[derive(Debug)]
19pub enum Error {
20    /// Peer error
21    Peer(PeerError),
22    /// Io error
23    Io(IoError),
24    /// Error from tentacle
25    P2P(P2PError),
26    /// Dail error
27    Dial(String),
28    /// Peer store error
29    PeerStore(PeerStoreError),
30}
31
32/// Error from tentacle
33#[derive(Debug)]
34pub enum P2PError {
35    /// Not support transport or some other error
36    Transport(TransportErrorKind),
37    /// Handle panic or other error
38    Protocol(ProtocolHandleErrorKind),
39    /// Dail error
40    Dail(DialerErrorKind),
41    /// Listen error
42    Listen(ListenErrorKind),
43    /// Net shutdown or too many messages blocked on
44    Send(SendErrorKind),
45}
46
47/// Peer store error
48#[derive(Debug)]
49pub enum PeerStoreError {
50    /// Indicate the peer store is full
51    EvictionFailed,
52    /// File data is not json format
53    Serde(serde_json::Error),
54}
55
56/// Peer error
57#[derive(Debug, Eq, PartialEq)]
58pub enum PeerError {
59    /// Session already exists
60    SessionExists(SessionId),
61    /// Peer id exist
62    PeerIdExists(PeerId),
63    /// Non-reserved peers
64    NonReserved,
65    /// Peer is banned
66    Banned,
67    /// Reach max inbound limit
68    ReachMaxInboundLimit,
69    /// Reach max outbound limit
70    ReachMaxOutboundLimit,
71}
72
73impl From<PeerStoreError> for Error {
74    fn from(err: PeerStoreError) -> Error {
75        Error::PeerStore(err)
76    }
77}
78
79impl From<PeerError> for Error {
80    fn from(err: PeerError) -> Error {
81        Error::Peer(err)
82    }
83}
84
85impl From<IoError> for Error {
86    fn from(err: IoError) -> Error {
87        Error::Io(err)
88    }
89}
90
91impl From<P2PError> for Error {
92    fn from(err: P2PError) -> Error {
93        Error::P2P(err)
94    }
95}
96
97impl From<TransportErrorKind> for Error {
98    fn from(err: TransportErrorKind) -> Error {
99        Error::P2P(P2PError::Transport(err))
100    }
101}
102
103impl From<ProtocolHandleErrorKind> for Error {
104    fn from(err: ProtocolHandleErrorKind) -> Error {
105        Error::P2P(P2PError::Protocol(err))
106    }
107}
108
109impl From<DialerErrorKind> for Error {
110    fn from(err: DialerErrorKind) -> Error {
111        Error::P2P(P2PError::Dail(err))
112    }
113}
114
115impl From<ListenErrorKind> for Error {
116    fn from(err: ListenErrorKind) -> Error {
117        Error::P2P(P2PError::Listen(err))
118    }
119}
120
121impl From<SendErrorKind> for Error {
122    fn from(err: SendErrorKind) -> Error {
123        Error::P2P(P2PError::Send(err))
124    }
125}
126
127impl Display for Error {
128    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
129        write!(f, "{self:?}")
130    }
131}
132
133impl Display for PeerError {
134    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
135        write!(f, "{self:?}")
136    }
137}
138
139impl Display for P2PError {
140    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
141        write!(f, "{self:?}")
142    }
143}