1use crate::types::MessagingError;
4use thiserror::Error;
5
6pub type Result<T> = std::result::Result<T, PgmqNotifyError>;
8
9#[derive(Error, Debug)]
11pub enum PgmqNotifyError {
12 #[error("Database error: {0}")]
14 Database(#[from] sqlx::Error),
15
16 #[error("Serialization error: {0}")]
18 Serialization(#[from] serde_json::Error),
19
20 #[error("Configuration error: {message}")]
22 Configuration { message: String },
23
24 #[error("Invalid channel name: {channel}")]
26 InvalidChannel { channel: String },
27
28 #[error("Invalid queue name pattern: {pattern}")]
30 InvalidPattern { pattern: String },
31
32 #[error("Listener is not connected to database")]
34 NotConnected,
35
36 #[error("Already listening to channel: {channel}")]
38 AlreadyListening { channel: String },
39
40 #[error("Regex error: {0}")]
42 Regex(#[from] regex::Error),
43
44 #[error("Generic error: {0}")]
46 Generic(#[from] anyhow::Error),
47
48 #[error("Messaging error: {0}")]
50 Messaging(#[from] MessagingError),
51
52 #[error("PGMQ error: {0}")]
54 Pgmq(#[from] pgmq::errors::PgmqError),
55}
56
57impl PgmqNotifyError {
58 pub fn config<S: Into<String>>(message: S) -> Self {
60 Self::Configuration {
61 message: message.into(),
62 }
63 }
64
65 pub fn invalid_channel<S: Into<String>>(channel: S) -> Self {
67 Self::InvalidChannel {
68 channel: channel.into(),
69 }
70 }
71
72 pub fn invalid_pattern<S: Into<String>>(pattern: S) -> Self {
74 Self::InvalidPattern {
75 pattern: pattern.into(),
76 }
77 }
78
79 pub fn already_listening<S: Into<String>>(channel: S) -> Self {
81 Self::AlreadyListening {
82 channel: channel.into(),
83 }
84 }
85}