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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use crate::{channel_status::ChannelState, connection_status::ConnectionState};
use amq_protocol::{frame::GenError, protocol::AMQPClass};
use std::{error, fmt, io};

/// A std Result with a lapin::Error error type
pub type Result<T> = std::result::Result<T, Error>;

/// The type of error that can be returned in this crate.
///
/// Even though we expose the complete enumeration of possible error variants, it is not
/// considered stable to exhaustively match on this enumeration: do it at your own risk.
#[derive(Debug)]
pub enum Error {
    InvalidMethod(AMQPClass),
    InvalidChannel(u16),
    ConnectionRefused,
    NotConnected,
    UnexpectedReply,
    PreconditionFailed,
    ChannelLimitReached,
    InvalidChannelState(ChannelState),
    InvalidConnectionState(ConnectionState),
    ParsingError(String),
    SerialisationError(GenError),
    IOError(io::Error),
    /// A hack to prevent developers from exhaustively match on the enum's variants
    ///
    /// The purpose of this variant is to let the `Error` enumeration grow more variants
    /// without it being a breaking change for users. It is planned for the language to provide
    /// this functionnality out of the box, though it has not been [stabilized] yet.
    ///
    /// [stabilized]: https://github.com/rust-lang/rust/issues/44109
    #[doc(hidden)]
    __Nonexhaustive,
}

impl Error {
    pub fn wouldblock(&self) -> bool {
        if let Error::IOError(e) = self {
            e.kind() == io::ErrorKind::WouldBlock
        } else {
            false
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::InvalidMethod(method) => write!(f, "invalid protocol method: {:?}", method),
            Error::InvalidChannel(channel) => write!(f, "invalid channel: {}", channel),
            Error::ConnectionRefused => write!(f, "connection refused"),
            Error::NotConnected => write!(f, "not connected"),
            Error::UnexpectedReply => write!(f, "unexpected reply"),
            Error::PreconditionFailed => write!(f, "precondition failed"),
            Error::ChannelLimitReached => write!(
                f,
                "The maximum number of channels for this connection has been reached"
            ),
            Error::InvalidChannelState(state) => write!(f, "invalid channel state: {:?}", state),
            Error::InvalidConnectionState(state) => {
                write!(f, "invalid connection state: {:?}", state)
            }
            Error::ParsingError(e) => write!(f, "Failed to parse: {}", e),
            Error::SerialisationError(e) => write!(f, "Failed to serialise: {:?}", e),
            Error::IOError(e) => write!(f, "IO error: {:?}", e),
            Error::__Nonexhaustive => write!(
                f,
                "lapin::Error::__Nonexhaustive: this should not be printed"
            ),
        }
    }
}

impl error::Error for Error {
    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
        match self {
            Error::SerialisationError(e) => Some(e),
            Error::IOError(e) => Some(e),
            _ => None,
        }
    }
}