Skip to main content

tasker_pgmq/
error.rs

1//! Error types for tasker-pgmq
2
3use crate::types::MessagingError;
4use thiserror::Error;
5
6/// Result type for tasker-pgmq operations
7pub type Result<T> = std::result::Result<T, PgmqNotifyError>;
8
9/// Errors that can occur in tasker-pgmq operations
10#[derive(Error, Debug)]
11pub enum PgmqNotifyError {
12    /// Database connection or query errors
13    #[error("Database error: {0}")]
14    Database(#[from] sqlx::Error),
15
16    /// JSON serialization/deserialization errors
17    #[error("Serialization error: {0}")]
18    Serialization(#[from] serde_json::Error),
19
20    /// Invalid configuration
21    #[error("Configuration error: {message}")]
22    Configuration { message: String },
23
24    /// Invalid channel name
25    #[error("Invalid channel name: {channel}")]
26    InvalidChannel { channel: String },
27
28    /// Invalid queue name pattern
29    #[error("Invalid queue name pattern: {pattern}")]
30    InvalidPattern { pattern: String },
31
32    /// Listener not connected
33    #[error("Listener is not connected to database")]
34    NotConnected,
35
36    /// Channel already being listened to
37    #[error("Already listening to channel: {channel}")]
38    AlreadyListening { channel: String },
39
40    /// Regex compilation error
41    #[error("Regex error: {0}")]
42    Regex(#[from] regex::Error),
43
44    /// Generic error for compatibility
45    #[error("Generic error: {0}")]
46    Generic(#[from] anyhow::Error),
47
48    /// Messaging error for compatibility with tasker-shared
49    #[error("Messaging error: {0}")]
50    Messaging(#[from] MessagingError),
51
52    /// PGMQ error from underlying library
53    #[error("PGMQ error: {0}")]
54    Pgmq(#[from] pgmq::errors::PgmqError),
55}
56
57impl PgmqNotifyError {
58    /// Create a configuration error
59    pub fn config<S: Into<String>>(message: S) -> Self {
60        Self::Configuration {
61            message: message.into(),
62        }
63    }
64
65    /// Create an invalid channel error
66    pub fn invalid_channel<S: Into<String>>(channel: S) -> Self {
67        Self::InvalidChannel {
68            channel: channel.into(),
69        }
70    }
71
72    /// Create an invalid pattern error
73    pub fn invalid_pattern<S: Into<String>>(pattern: S) -> Self {
74        Self::InvalidPattern {
75            pattern: pattern.into(),
76        }
77    }
78
79    /// Create an already listening error
80    pub fn already_listening<S: Into<String>>(channel: S) -> Self {
81        Self::AlreadyListening {
82            channel: channel.into(),
83        }
84    }
85}