Skip to main content

ruststream_fred/
error.rs

1//! Error type returned by Redis broker operations.
2
3use std::error::Error as StdError;
4
5use thiserror::Error;
6
7/// Errors surfaced by the Redis broker implementation.
8#[derive(Debug, Error)]
9#[non_exhaustive]
10pub enum RedisError {
11    /// Failed to establish or use the underlying `fred` connection.
12    #[error("redis connection error: {0}")]
13    Connect(#[source] Box<dyn StdError + Send + Sync>),
14
15    /// Failed to publish (`XADD`) a message to a stream.
16    #[error("redis publish error: {0}")]
17    Publish(#[source] Box<dyn StdError + Send + Sync>),
18
19    /// Failed to open a subscription (consumer-group creation or the first read).
20    #[error("redis subscribe error: {0}")]
21    Subscribe(#[source] Box<dyn StdError + Send + Sync>),
22
23    /// A stream read (`XREADGROUP` / `XAUTOCLAIM`) or acknowledgement (`XACK`) failed.
24    #[error("redis stream error: {0}")]
25    Stream(#[source] Box<dyn StdError + Send + Sync>),
26
27    /// An operation needing a live connection ran before [`crate::RedisBroker`] was connected.
28    ///
29    /// A broker built with [`RedisBroker::standalone`](crate::RedisBroker::standalone) connects
30    /// lazily: the runtime calls [`Broker::connect`](ruststream::Broker::connect) at startup.
31    /// Publishing or subscribing before that returns this error.
32    #[error("redis broker is not connected")]
33    NotConnected,
34
35    /// The supplied subscription descriptor combines fields in a way the broker cannot honour
36    /// (for example a [`RedisStream`](crate::RedisStream) with no consumer group, or a bare-string
37    /// subscription with no broker-wide default group).
38    #[error("invalid subscribe options: {0}")]
39    InvalidOptions(String),
40}
41
42impl RedisError {
43    /// Wraps a `fred` error as a [`RedisError::Stream`].
44    pub(crate) fn stream(err: fred::error::Error) -> Self {
45        Self::Stream(Box::new(err))
46    }
47
48    /// Wraps a `fred` error as a [`RedisError::Subscribe`].
49    pub(crate) fn subscribe(err: fred::error::Error) -> Self {
50        Self::Subscribe(Box::new(err))
51    }
52
53    /// Wraps a `fred` error as a [`RedisError::Publish`].
54    pub(crate) fn publish(err: fred::error::Error) -> Self {
55        Self::Publish(Box::new(err))
56    }
57}