Skip to main content

oxisql_core/
error.rs

1//! Error type for OxiSQL operations.
2
3use std::fmt;
4
5/// Errors that can occur during OxiSQL operations.
6#[derive(Debug)]
7pub enum OxiSqlError {
8    /// The SQL could not be parsed.
9    Parse(String),
10    /// The statement failed during execution.
11    Execution(String),
12    /// No connection is available.
13    NotConnected,
14    /// A value had an unexpected type.
15    TypeMismatch {
16        /// The type that was expected.
17        expected: &'static str,
18        /// The type that was encountered.
19        got: &'static str,
20    },
21    /// A unique or foreign key constraint was violated.
22    ConstraintViolation(String),
23    /// A connection or query timeout occurred.
24    Timeout(String),
25    /// A connection pool error (exhausted, timeout, build failure).
26    ConnectionPool(String),
27    /// A migration error.
28    Migration(String),
29    /// The requested URI or feature is not supported by this backend.
30    UnsupportedUri(String),
31    /// A named-parameter binding error (missing or malformed placeholder).
32    Params(String),
33    /// Any other error.
34    Other(String),
35}
36
37impl fmt::Display for OxiSqlError {
38    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39        match self {
40            OxiSqlError::Parse(s) => write!(f, "SQL parse error: {s}"),
41            OxiSqlError::Execution(s) => write!(f, "execution error: {s}"),
42            OxiSqlError::NotConnected => write!(f, "not connected"),
43            OxiSqlError::TypeMismatch { expected, got } => {
44                write!(f, "type mismatch: expected {expected}, got {got}")
45            }
46            OxiSqlError::ConstraintViolation(s) => {
47                write!(f, "constraint violation: {s}")
48            }
49            OxiSqlError::Timeout(s) => write!(f, "timeout: {s}"),
50            OxiSqlError::ConnectionPool(s) => write!(f, "connection pool error: {s}"),
51            OxiSqlError::Migration(s) => write!(f, "migration error: {s}"),
52            OxiSqlError::UnsupportedUri(s) => write!(f, "unsupported URI or feature: {s}"),
53            OxiSqlError::Params(s) => write!(f, "parameter binding error: {s}"),
54            OxiSqlError::Other(s) => write!(f, "error: {s}"),
55        }
56    }
57}
58
59impl std::error::Error for OxiSqlError {}