Skip to main content

rustbasic_core/sql/driver/
error.rs

1use std::fmt;
2
3#[derive(Debug)]
4pub enum SqlError {
5    Io(std::io::Error),
6    Protocol(String),
7    Server {
8        code: u16,
9        sql_state: String,
10        message: String,
11    },
12    RowNotFound,
13    ColumnNotFound(String),
14    ColumnIndexOutOfBounds { len: usize, index: usize },
15    Decode(String),
16    Other(String),
17}
18
19impl std::error::Error for SqlError {
20    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
21        match self {
22            SqlError::Io(e) => Some(e),
23            _ => None,
24        }
25    }
26}
27
28impl fmt::Display for SqlError {
29    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30        match self {
31            SqlError::Io(e) => write!(f, "I/O error: {}", e),
32            SqlError::Protocol(s) => write!(f, "MySQL protocol error: {}", s),
33            SqlError::Server { code, sql_state, message } => {
34                write!(f, "MySQL server error (code {}): [{}] {}", code, sql_state, message)
35            }
36            SqlError::RowNotFound => write!(f, "Row not found"),
37            SqlError::ColumnNotFound(name) => write!(f, "Column not found: '{}'", name),
38            SqlError::ColumnIndexOutOfBounds { len, index } => {
39                write!(f, "Column index out of bounds: length is {}, index is {}", len, index)
40            }
41            SqlError::Decode(s) => write!(f, "Decode error: {}", s),
42            SqlError::Other(s) => write!(f, "Other error: {}", s),
43        }
44    }
45}
46
47impl From<std::io::Error> for SqlError {
48    fn from(err: std::io::Error) -> Self {
49        SqlError::Io(err)
50    }
51}