lapin_async/
error.rs

1use amq_protocol::{
2  frame::GenError,
3  protocol::AMQPClass,
4};
5use failure::{Backtrace, Context, Fail};
6
7use std::{fmt, io};
8
9use crate::connection_status::ConnectionState;
10
11/// The type of error that can be returned in this crate.
12///
13/// Instead of implementing the `Error` trait provided by the standard library,
14/// it implemented the `Fail` trait provided by the `failure` crate. Doing so
15/// means that this type guaranteed to be both sendable and usable across
16/// threads, and that you'll be able to use the downcasting feature of the
17/// `failure::Error` type.
18#[derive(Debug)]
19#[deprecated(note = "use lapin instead")]
20pub struct Error {
21  inner: Context<ErrorKind>,
22}
23
24/// The different kinds of errors that can be reported.
25///
26/// Even though we expose the complete enumeration of possible error variants, it is not
27/// considered stable to exhaustively match on this enumeration: do it at your own risk.
28#[derive(Debug, Fail)]
29#[deprecated(note = "use lapin instead")]
30pub enum ErrorKind {
31  #[fail(display = "invalid protocol method: {:?}", _0)]
32  InvalidMethod(AMQPClass),
33  #[fail(display = "invalid channel: {}", _0)]
34  InvalidChannel(u16),
35  #[fail(display = "connection refused")]
36  ConnectionRefused,
37  #[fail(display = "not connected")]
38  NotConnected,
39  #[fail(display = "unexpected reply")]
40  UnexpectedReply,
41  #[fail(display = "precondition failed")]
42  PreconditionFailed,
43  #[fail(display = "The maximum number of channels for this connection has been reached")]
44  ChannelLimitReached,
45  #[fail(display = "invalid connection state: {:?}", _0)]
46  InvalidConnectionState(ConnectionState),
47  #[fail(display = "Failed to parse: {}", _0)]
48  ParsingError(String),
49  #[fail(display = "Failed to serialise: {:?}", _0)]
50  SerialisationError(#[fail(cause)] GenError),
51  #[fail(display = "IO error: {:?}", _0)]
52  IOError(#[fail(cause)] io::Error),
53  #[fail(display = "IO loop error")]
54  IoLoopError,
55  /// A hack to prevent developers from exhaustively match on the enum's variants
56  ///
57  /// The purpose of this variant is to let the `ErrorKind` enumeration grow more variants
58  /// without it being a breaking change for users. It is planned for the language to provide
59  /// this functionnality out of the box, though it has not been [stabilized] yet.
60  ///
61  /// [stabilized]: https://github.com/rust-lang/rust/issues/44109
62  #[doc(hidden)]
63  #[fail(display = "lapin_async::error::ErrorKind::__Nonexhaustive: this should not be printed")]
64  __Nonexhaustive,
65}
66
67impl Error {
68  /// Return the underlying `ErrorKind`
69  #[deprecated(note = "use lapin instead")]
70  pub fn kind(&self) -> &ErrorKind {
71    self.inner.get_context()
72  }
73}
74
75impl Fail for Error {
76  fn cause(&self) -> Option<&dyn Fail> {
77    self.inner.cause()
78  }
79
80  fn backtrace(&self) -> Option<&Backtrace> {
81    self.inner.backtrace()
82  }
83}
84
85impl fmt::Display for Error {
86  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
87    fmt::Display::fmt(&self.inner, f)
88  }
89}
90
91impl From<ErrorKind> for Error {
92  fn from(kind: ErrorKind) -> Error {
93    Error { inner: Context::new(kind) }
94  }
95}
96
97impl From<Context<ErrorKind>> for Error {
98  fn from(inner: Context<ErrorKind>) -> Error {
99    Error { inner }
100  }
101}