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, ack, etc.).
24    #[error("nats jetstream error: {0}")]
25    JetStream(#[source] Box<dyn StdError + Send + Sync>),
26
27    /// A request / reply operation timed out before a reply was received.
28    #[error("nats request timed out")]
29    RequestTimeout,
30
31    /// An operation needing a live connection ran before [`crate::NatsBroker`] was connected.
32    ///
33    /// A broker built with [`NatsBroker::new`](crate::NatsBroker::new) connects lazily: the runtime
34    /// calls [`Broker::connect`](ruststream::Broker::connect) at startup. Publishing or subscribing
35    /// before that returns this error.
36    #[error("nats broker is not connected")]
37    NotConnected,
38
39    /// The supplied [`crate::SubscribeOptions`] combine fields in a way the broker cannot honour
40    /// (for example `durable(_)` without `jetstream(_)`, or `queue_group(_)` together with
41    /// `jetstream(_)`).
42    #[error("invalid subscribe options: {0}")]
43    InvalidOptions(String),
44}