libp2p_plaintext/
error.rs

1// Copyright 2019 Parity Technologies (UK) Ltd.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the "Software"),
5// to deal in the Software without restriction, including without limitation
6// the rights to use, copy, modify, merge, publish, distribute, sublicense,
7// and/or sell copies of the Software, and to permit persons to whom the
8// Software is furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
19// DEALINGS IN THE SOFTWARE.
20
21use std::{error, fmt, io::Error as IoError};
22
23#[derive(Debug)]
24pub enum Error {
25    /// I/O error.
26    Io(IoError),
27
28    /// Failed to parse the handshake protobuf message.
29    InvalidPayload(DecodeError),
30
31    /// Failed to parse public key from bytes in protobuf message.
32    InvalidPublicKey(libp2p_identity::DecodingError),
33
34    /// Failed to parse the [`PeerId`](libp2p_identity::PeerId) from bytes in the protobuf message.
35    InvalidPeerId(libp2p_identity::ParseError),
36
37    /// The peer id of the exchange isn't consistent with the remote public key.
38    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}