libp2p_plaintext/
error.rs1use std::{error, fmt, io::Error as IoError};
22
23#[derive(Debug)]
24pub enum Error {
25 Io(IoError),
27
28 InvalidPayload(DecodeError),
30
31 InvalidPublicKey(libp2p_identity::DecodingError),
33
34 InvalidPeerId(libp2p_identity::ParseError),
36
37 PeerIdMismatch,
39}
40
41#[derive(Debug)]
42pub struct DecodeError(pub(crate) quick_protobuf_codec::Error);
43
44impl fmt::Display for DecodeError {
45 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46 self.0.fmt(f)
47 }
48}
49
50impl error::Error for DecodeError {
51 fn source(&self) -> Option<&(dyn error::Error + 'static)> {
52 self.0.source()
53 }
54}
55
56impl error::Error for Error {
57 fn cause(&self) -> Option<&dyn error::Error> {
58 match *self {
59 Error::Io(ref err) => Some(err),
60 Error::InvalidPayload(ref err) => Some(err),
61 Error::InvalidPublicKey(ref err) => Some(err),
62 Error::InvalidPeerId(ref err) => Some(err),
63 _ => None,
64 }
65 }
66}
67
68impl fmt::Display for Error {
69 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
70 match self {
71 Error::Io(e) => write!(f, "I/O error: {e}"),
72 Error::InvalidPayload(_) => f.write_str("Failed to decode protobuf"),
73 Error::PeerIdMismatch => f.write_str(
74 "The peer id of the exchange isn't consistent with the remote public key",
75 ),
76 Error::InvalidPublicKey(_) => f.write_str("Failed to decode public key"),
77 Error::InvalidPeerId(_) => f.write_str("Failed to decode PeerId"),
78 }
79 }
80}
81
82impl From<IoError> for Error {
83 fn from(err: IoError) -> Error {
84 Error::Io(err)
85 }
86}
87
88impl From<DecodeError> for Error {
89 fn from(err: DecodeError) -> Error {
90 Error::InvalidPayload(err)
91 }
92}
93
94impl From<libp2p_identity::DecodingError> for Error {
95 fn from(err: libp2p_identity::DecodingError) -> Error {
96 Error::InvalidPublicKey(err)
97 }
98}
99
100impl From<libp2p_identity::ParseError> for Error {
101 fn from(err: libp2p_identity::ParseError) -> Error {
102 Error::InvalidPeerId(err)
103 }
104}