Skip to main content

prax_postgres/
error.rs

1//! Error types for PostgreSQL operations.
2
3use prax_query::QueryError;
4use thiserror::Error;
5
6/// Result type for PostgreSQL operations.
7pub type PgResult<T> = Result<T, PgError>;
8
9/// Errors that can occur during PostgreSQL operations.
10#[derive(Error, Debug)]
11pub enum PgError {
12    /// Connection pool error.
13    #[error("pool error: {0}")]
14    Pool(#[from] deadpool_postgres::PoolError),
15
16    /// PostgreSQL error.
17    #[error("postgres error: {0}")]
18    Postgres(#[from] tokio_postgres::Error),
19
20    /// Configuration error.
21    #[error("configuration error: {0}")]
22    Config(String),
23
24    /// Connection error.
25    #[error("connection error: {0}")]
26    Connection(String),
27
28    /// Query execution error.
29    #[error("query error: {0}")]
30    Query(String),
31
32    /// Row deserialization error.
33    #[error("deserialization error: {0}")]
34    Deserialization(String),
35
36    /// Type conversion error.
37    #[error("type conversion error: {0}")]
38    TypeConversion(String),
39
40    /// Timeout error.
41    #[error("operation timed out after {0}ms")]
42    Timeout(u64),
43
44    /// Internal error.
45    #[error("internal error: {0}")]
46    Internal(String),
47}
48
49impl PgError {
50    /// Create a configuration error.
51    pub fn config(message: impl Into<String>) -> Self {
52        Self::Config(message.into())
53    }
54
55    /// Create a connection error.
56    pub fn connection(message: impl Into<String>) -> Self {
57        Self::Connection(message.into())
58    }
59
60    /// Create a query error.
61    pub fn query(message: impl Into<String>) -> Self {
62        Self::Query(message.into())
63    }
64
65    /// Create a deserialization error.
66    pub fn deserialization(message: impl Into<String>) -> Self {
67        Self::Deserialization(message.into())
68    }
69
70    /// Create a type conversion error.
71    pub fn type_conversion(message: impl Into<String>) -> Self {
72        Self::TypeConversion(message.into())
73    }
74
75    /// Check if this is a connection error.
76    pub fn is_connection_error(&self) -> bool {
77        matches!(self, Self::Pool(_) | Self::Connection(_))
78    }
79
80    /// Check if this is a timeout error.
81    pub fn is_timeout(&self) -> bool {
82        matches!(self, Self::Timeout(_))
83    }
84}
85
86impl From<PgError> for QueryError {
87    fn from(err: PgError) -> Self {
88        match err {
89            PgError::Pool(e) => QueryError::connection(e.to_string()),
90            PgError::Postgres(e) => {
91                // Categorize by SQLSTATE while preserving the driver error
92                // as the source.
93                let code_str = e.code().map(|c| c.code().to_owned());
94                let msg = e.to_string();
95                let mapped = match code_str.as_deref() {
96                    // Unique violation
97                    Some("23505") => QueryError::constraint_violation("", msg),
98                    // Foreign key violation
99                    Some("23503") => QueryError::constraint_violation("", msg),
100                    // Not null violation
101                    Some("23502") => QueryError::invalid_input("", msg),
102                    _ => QueryError::database(msg),
103                };
104                mapped.with_source(e)
105            }
106            PgError::Config(msg) => QueryError::connection(msg),
107            PgError::Connection(msg) => QueryError::connection(msg),
108            PgError::Query(msg) => QueryError::database(msg),
109            PgError::Deserialization(msg) => QueryError::serialization(msg),
110            PgError::TypeConversion(msg) => QueryError::serialization(msg),
111            PgError::Timeout(ms) => QueryError::timeout(ms),
112            PgError::Internal(msg) => QueryError::internal(msg),
113        }
114    }
115}
116
117#[cfg(test)]
118mod tests {
119    use super::*;
120
121    #[test]
122    fn test_error_creation() {
123        let err = PgError::config("invalid URL");
124        assert!(matches!(err, PgError::Config(_)));
125
126        let err = PgError::connection("connection refused");
127        assert!(err.is_connection_error());
128
129        let err = PgError::Timeout(5000);
130        assert!(err.is_timeout());
131    }
132
133    #[test]
134    fn test_into_query_error() {
135        let pg_err = PgError::Timeout(1000);
136        let query_err: QueryError = pg_err.into();
137        assert!(query_err.is_timeout());
138    }
139}