Skip to main content

ruststream_nats/
error.rs

1//! Error type returned by NATS broker operations.
2
3use std::error::Error as StdError;
4
5use thiserror::Error;
6
7/// Errors surfaced by the NATS broker implementation.
8#[derive(Debug, Error)]
9#[non_exhaustive]
10pub enum NatsError {
11    /// Failed to establish or use the underlying `async-nats` connection.
12    #[error("nats connection error: {0}")]
13    Connect(#[source] Box<dyn StdError + Send + Sync>),
14
15    /// Failed to publish a message to the broker.
16    #[error("nats publish error: {0}")]
17    Publish(#[source] Box<dyn StdError + Send + Sync>),
18
19    /// Failed to subscribe to the requested subject.
20    #[error("nats subscribe error: {0}")]
21    Subscribe(#[source] Box<dyn StdError + Send + Sync>),
22
23    /// JetStream-specific operation failed (consumer creation, publish acknowledgement, ack).
24    #[error("nats jetstream error: {0}")]
25    JetStream(#[source] Box<dyn StdError + Send + Sync>),
26
27    /// Draining the connection during
28    /// [`shutdown`](ruststream::ConnectedBroker::shutdown) failed.
29    #[error("nats shutdown error: {0}")]
30    Shutdown(#[source] Box<dyn StdError + Send + Sync>),
31
32    /// A request / reply operation timed out before a reply was received.
33    #[error("nats request timed out")]
34    RequestTimeout,
35
36    /// A publisher aliasing the connection was used after the broker shut down.
37    ///
38    /// The lifecycle ladder makes misuse through the owner's handle a compile error:
39    /// [`ConnectedBroker::shutdown`](ruststream::ConnectedBroker::shutdown) consumes the connected
40    /// broker. Publishers paired off it earlier keep aliasing the closed connection, so their
41    /// operations report this instead of silently succeeding against a dead connection.
42    #[error("nats connection is closed; cannot reach {subject}")]
43    Closed {
44        /// The subject the closed publisher was asked to reach.
45        subject: String,
46    },
47
48    /// The supplied [`crate::SubscribeOptions`] combine fields in a way the broker cannot honour
49    /// (for example `durable(_)` without `jetstream(_)`, or `queue_group(_)` together with
50    /// `jetstream(_)`).
51    #[error("invalid subscribe options: {0}")]
52    InvalidOptions(String),
53}