Skip to main content

ruststream_lapin/
error.rs

1//! The crate error type shared by broker, publishers, subscribers, and the requester.
2
3use std::error::Error as StdError;
4use std::time::Duration;
5
6use thiserror::Error;
7
8/// Errors returned by [`LapinBroker`](crate::LapinBroker) and the types it hands out.
9///
10/// Underlying [`lapin`](https://docs.rs/lapin) errors are boxed as sources so the client library
11/// does not leak into this crate's public API surface.
12#[derive(Debug, Error)]
13#[non_exhaustive]
14pub enum AmqpError {
15    /// Establishing or closing the connection failed.
16    #[error("amqp connection error: {0}")]
17    Connect(#[source] Box<dyn StdError + Send + Sync>),
18
19    /// Publishing a message failed, or the broker refused to confirm it.
20    #[error("amqp publish error: {0}")]
21    Publish(#[source] Box<dyn StdError + Send + Sync>),
22
23    /// Opening a subscription (channel, `QoS`, or consume) failed.
24    #[error("amqp subscribe error: {0}")]
25    Subscribe(#[source] Box<dyn StdError + Send + Sync>),
26
27    /// Receiving a delivery from an open consumer failed.
28    #[error("amqp consume error: {0}")]
29    Consume(#[source] Box<dyn StdError + Send + Sync>),
30
31    /// Declaring the expected topology (exchange, queue, or binding) failed.
32    #[error("amqp topology declaration error: {0}")]
33    Declare(#[source] Box<dyn StdError + Send + Sync>),
34
35    /// Sending a request or receiving its reply failed.
36    #[error("amqp request error: {0}")]
37    Request(#[source] Box<dyn StdError + Send + Sync>),
38
39    /// No reply arrived within the caller's deadline.
40    ///
41    /// The pending request is dropped; a reply arriving later is discarded.
42    #[error("amqp request timed out after {0:?} without a reply")]
43    RequestTimeout(Duration),
44
45    /// An operation needed the live connection before `Broker::connect` resolved it.
46    ///
47    /// The runtime connects the broker once at startup; a publisher handed out earlier resolves
48    /// the shared connection on first use. Seeing this error means the operation ran before
49    /// `connect` completed (or after `shutdown`).
50    #[error("amqp broker is not connected; `Broker::connect` must complete first")]
51    NotConnected,
52
53    /// The requested combination of options cannot be executed.
54    ///
55    /// The message names the offending option and the remediation.
56    #[error("invalid options: {0}")]
57    InvalidOptions(String),
58}
59
60impl AmqpError {
61    pub(crate) fn connect(err: lapin::Error) -> Self {
62        Self::Connect(Box::new(err))
63    }
64
65    pub(crate) fn publish(err: lapin::Error) -> Self {
66        Self::Publish(Box::new(err))
67    }
68
69    pub(crate) fn subscribe(err: lapin::Error) -> Self {
70        Self::Subscribe(Box::new(err))
71    }
72
73    pub(crate) fn consume(err: lapin::Error) -> Self {
74        Self::Consume(Box::new(err))
75    }
76
77    pub(crate) fn declare(err: lapin::Error) -> Self {
78        Self::Declare(Box::new(err))
79    }
80
81    pub(crate) fn request(err: lapin::Error) -> Self {
82        Self::Request(Box::new(err))
83    }
84}