Skip to main content

nest_rs_queue/
error.rs

1//! Typed errors for the queue producer surface.
2//!
3//! Framework crates surface `thiserror` enums, not `anyhow`. An enqueue can
4//! fail two ways: serializing the job to its JSON wire form, or inside the
5//! backend's push. The backend failure is kept behind a boxed `source` so this
6//! contract names no concrete backend — a Redis backend wraps its apalis/Redis
7//! error, an SQS backend its SDK error, without this crate depending on either.
8
9use thiserror::Error;
10
11/// A failure enqueuing a job through a [`JobProducer`](crate::JobProducer) (or
12/// the [`push`](crate::JobProducerExt::push) convenience over it).
13#[derive(Debug, Error)]
14#[non_exhaustive]
15pub enum QueueError {
16    /// The job could not be serialized to its JSON wire form.
17    #[error("failed to serialize job payload")]
18    Serialize(#[from] serde_json::Error),
19    /// The backend rejected or failed the enqueue. The concrete backend
20    /// failure is the `source`, kept opaque so the producer contract stays
21    /// backend-agnostic.
22    #[error("queue backend failed to enqueue job")]
23    Backend(#[source] Box<dyn std::error::Error + Send + Sync>),
24}
25
26impl QueueError {
27    /// Wrap a backend-specific enqueue failure as [`QueueError::Backend`]. A
28    /// backend calls this to surface its concrete error (an apalis/Redis error,
29    /// an SQS SDK error, …) without this crate naming the type — e.g.
30    /// `storage.push(job).await.map_err(QueueError::backend)?`.
31    pub fn backend<E>(source: E) -> Self
32    where
33        E: std::error::Error + Send + Sync + 'static,
34    {
35        Self::Backend(Box::new(source))
36    }
37}