Skip to main content

pgorm_check/
error.rs

1//! Error types for pgorm-check
2
3use std::fmt;
4
5/// Result type for pgorm-check operations.
6pub type CheckResult<T> = Result<T, CheckError>;
7
8/// Error type for pgorm-check operations.
9#[derive(Debug)]
10pub enum CheckError {
11    /// Database error from tokio-postgres.
12    Database(tokio_postgres::Error),
13    /// Validation error (e.g., SQL parse error, missing schema).
14    Validation(String),
15    /// Serialization/deserialization error.
16    Serialization(String),
17    /// Decode error when reading a column.
18    Decode { column: String, message: String },
19    /// IO or other error.
20    Other(String),
21}
22
23impl fmt::Display for CheckError {
24    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
25        match self {
26            CheckError::Database(e) => write!(f, "Database error: {e}"),
27            CheckError::Validation(msg) => write!(f, "Validation error: {msg}"),
28            CheckError::Serialization(msg) => write!(f, "Serialization error: {msg}"),
29            CheckError::Decode { column, message } => {
30                write!(f, "Decode error for column '{column}': {message}")
31            }
32            CheckError::Other(msg) => write!(f, "{msg}"),
33        }
34    }
35}
36
37impl std::error::Error for CheckError {
38    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
39        match self {
40            CheckError::Database(e) => Some(e),
41            _ => None,
42        }
43    }
44}
45
46impl From<tokio_postgres::Error> for CheckError {
47    fn from(e: tokio_postgres::Error) -> Self {
48        CheckError::Database(e)
49    }
50}
51
52impl CheckError {
53    /// Create a decode error.
54    pub fn decode(column: impl Into<String>, message: impl Into<String>) -> Self {
55        CheckError::Decode {
56            column: column.into(),
57            message: message.into(),
58        }
59    }
60}