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