use thiserror::Error;
use uuid::Uuid;
#[derive(Debug, Error)]
pub enum BrokerError {
#[error("Connection error: {0}")]
Connection(String),
#[error("Serialization error: {0}")]
Serialization(String),
#[error("Queue not found: {0}")]
QueueNotFound(String),
#[error("Message not found: {0}")]
MessageNotFound(Uuid),
#[error("Timeout waiting for message")]
Timeout,
#[error("Invalid configuration: {0}")]
Configuration(String),
#[error("Broker operation failed: {0}")]
OperationFailed(String),
}
impl BrokerError {
pub fn is_connection(&self) -> bool {
matches!(self, BrokerError::Connection(_))
}
pub fn is_serialization(&self) -> bool {
matches!(self, BrokerError::Serialization(_))
}
pub fn is_queue_not_found(&self) -> bool {
matches!(self, BrokerError::QueueNotFound(_))
}
pub fn is_message_not_found(&self) -> bool {
matches!(self, BrokerError::MessageNotFound(_))
}
pub fn is_timeout(&self) -> bool {
matches!(self, BrokerError::Timeout)
}
pub fn is_configuration(&self) -> bool {
matches!(self, BrokerError::Configuration(_))
}
pub fn is_operation_failed(&self) -> bool {
matches!(self, BrokerError::OperationFailed(_))
}
pub fn is_retryable(&self) -> bool {
matches!(
self,
BrokerError::Connection(_) | BrokerError::Timeout | BrokerError::OperationFailed(_)
)
}
pub fn category(&self) -> &'static str {
match self {
BrokerError::Connection(_) => "connection",
BrokerError::Serialization(_) => "serialization",
BrokerError::QueueNotFound(_) => "queue_not_found",
BrokerError::MessageNotFound(_) => "message_not_found",
BrokerError::Timeout => "timeout",
BrokerError::Configuration(_) => "configuration",
BrokerError::OperationFailed(_) => "operation_failed",
}
}
}
pub type Result<T> = std::result::Result<T, BrokerError>;