Skip to main content

celers_kombu/
error.rs

1//! Broker error types and result alias.
2
3use thiserror::Error;
4use uuid::Uuid;
5
6/// Broker errors
7///
8/// # Examples
9///
10/// ```
11/// use celers_kombu::BrokerError;
12///
13/// let err = BrokerError::Connection("failed to connect".to_string());
14/// assert!(err.is_connection());
15/// assert!(err.is_retryable());
16/// assert_eq!(err.category(), "connection");
17///
18/// let err = BrokerError::Timeout;
19/// assert!(err.is_timeout());
20/// assert!(err.is_retryable());
21///
22/// let err = BrokerError::QueueNotFound("celery".to_string());
23/// assert!(err.is_queue_not_found());
24/// assert!(!err.is_retryable());
25/// ```
26#[derive(Debug, Error)]
27pub enum BrokerError {
28    #[error("Connection error: {0}")]
29    Connection(String),
30
31    #[error("Serialization error: {0}")]
32    Serialization(String),
33
34    #[error("Queue not found: {0}")]
35    QueueNotFound(String),
36
37    #[error("Message not found: {0}")]
38    MessageNotFound(Uuid),
39
40    #[error("Timeout waiting for message")]
41    Timeout,
42
43    #[error("Invalid configuration: {0}")]
44    Configuration(String),
45
46    #[error("Broker operation failed: {0}")]
47    OperationFailed(String),
48}
49
50impl BrokerError {
51    /// Check if the error is connection-related
52    pub fn is_connection(&self) -> bool {
53        matches!(self, BrokerError::Connection(_))
54    }
55
56    /// Check if the error is serialization-related
57    pub fn is_serialization(&self) -> bool {
58        matches!(self, BrokerError::Serialization(_))
59    }
60
61    /// Check if the error is queue-not-found
62    pub fn is_queue_not_found(&self) -> bool {
63        matches!(self, BrokerError::QueueNotFound(_))
64    }
65
66    /// Check if the error is message-not-found
67    pub fn is_message_not_found(&self) -> bool {
68        matches!(self, BrokerError::MessageNotFound(_))
69    }
70
71    /// Check if the error is a timeout
72    pub fn is_timeout(&self) -> bool {
73        matches!(self, BrokerError::Timeout)
74    }
75
76    /// Check if the error is configuration-related
77    pub fn is_configuration(&self) -> bool {
78        matches!(self, BrokerError::Configuration(_))
79    }
80
81    /// Check if the error is an operation failure
82    pub fn is_operation_failed(&self) -> bool {
83        matches!(self, BrokerError::OperationFailed(_))
84    }
85
86    /// Check if this is a retryable error
87    ///
88    /// Returns true for connection, timeout, and operation failures, which are typically transient.
89    /// Returns false for serialization, configuration, and not-found errors.
90    pub fn is_retryable(&self) -> bool {
91        matches!(
92            self,
93            BrokerError::Connection(_) | BrokerError::Timeout | BrokerError::OperationFailed(_)
94        )
95    }
96
97    /// Get the error category as a string
98    pub fn category(&self) -> &'static str {
99        match self {
100            BrokerError::Connection(_) => "connection",
101            BrokerError::Serialization(_) => "serialization",
102            BrokerError::QueueNotFound(_) => "queue_not_found",
103            BrokerError::MessageNotFound(_) => "message_not_found",
104            BrokerError::Timeout => "timeout",
105            BrokerError::Configuration(_) => "configuration",
106            BrokerError::OperationFailed(_) => "operation_failed",
107        }
108    }
109}
110
111pub type Result<T> = std::result::Result<T, BrokerError>;