1use thiserror::Error;
4use uuid::Uuid;
5
6#[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 pub fn is_connection(&self) -> bool {
53 matches!(self, BrokerError::Connection(_))
54 }
55
56 pub fn is_serialization(&self) -> bool {
58 matches!(self, BrokerError::Serialization(_))
59 }
60
61 pub fn is_queue_not_found(&self) -> bool {
63 matches!(self, BrokerError::QueueNotFound(_))
64 }
65
66 pub fn is_message_not_found(&self) -> bool {
68 matches!(self, BrokerError::MessageNotFound(_))
69 }
70
71 pub fn is_timeout(&self) -> bool {
73 matches!(self, BrokerError::Timeout)
74 }
75
76 pub fn is_configuration(&self) -> bool {
78 matches!(self, BrokerError::Configuration(_))
79 }
80
81 pub fn is_operation_failed(&self) -> bool {
83 matches!(self, BrokerError::OperationFailed(_))
84 }
85
86 pub fn is_retryable(&self) -> bool {
91 matches!(
92 self,
93 BrokerError::Connection(_) | BrokerError::Timeout | BrokerError::OperationFailed(_)
94 )
95 }
96
97 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>;