1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use libp2p_identity::PeerId;
use multiaddr::Multiaddr;
use multistream_select::NegotiationError;

/// A error variant, returns by switch apis.
#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error("This switch does not support the transport binding/connecting with addr={0}")]
    UnspportMultiAddr(Multiaddr),

    #[error(transparent)]
    IoError(#[from] std::io::Error),

    #[error("Connect to peer, id={0}, error='route path is not exists'")]
    ConnectPeer(PeerId),

    #[error(transparent)]
    UnsignedVarint(#[from] unsigned_varint::io::ReadError),

    #[error("Received identity length > {0}")]
    IdentityOverflow(usize),

    #[error(transparent)]
    ProtobufError(#[from] protobuf::Error),

    #[error(transparent)]
    IdentityDecodingError(#[from] libp2p_identity::DecodingError),

    #[error("Identity check conn({0}) != identify({1})")]
    IdentityCheckFailed(PeerId, PeerId),

    #[error(transparent)]
    Multiaddr(#[from] multiaddr::Error),

    #[error(transparent)]
    NegotiationError(#[from] NegotiationError),

    #[error("Received invalid ping packet, len={0}")]
    InvalidPingLength(usize),

    #[error("Protocol timeout.")]
    Timeout,

    #[error("{0}")]
    Other(String),

    #[error("Can't bind listener on '{0}'")]
    BindError(String),

    #[error("Protocol listener is not exist or is closed, '{0}'")]
    ProtocolListener(usize),
}

/// The result type for this module.
pub type Result<T> = std::result::Result<T, Error>;

impl From<Error> for std::io::Error {
    fn from(value: Error) -> Self {
        match value {
            Error::IoError(err) => err,
            err => std::io::Error::new(std::io::ErrorKind::Other, err),
        }
    }
}