Skip to main content

regulus_db/types/
error.rs

1use crate::types::schema::SchemaError;
2use std::fmt;
3use std::io;
4
5/// 数据库错误类型
6#[derive(Debug)]
7pub enum DbError {
8    /// 表不存在
9    TableNotFound(String),
10    /// 表已存在
11    TableAlreadyExists(String),
12    /// 行不存在
13    RowNotFound,
14    /// Schema 错误
15    SchemaError(SchemaError),
16    /// 事务错误
17    TransactionError(String),
18    /// 索引错误
19    IndexError(String),
20    /// IO 错误
21    IoError(io::Error),
22    /// 内部错误
23    InternalError(String),
24    /// 其他错误
25    Other(String),
26}
27
28impl fmt::Display for DbError {
29    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30        match self {
31            DbError::TableNotFound(name) => write!(f, "Table not found: {}", name),
32            DbError::TableAlreadyExists(name) => write!(f, "Table already exists: {}", name),
33            DbError::RowNotFound => write!(f, "Row not found"),
34            DbError::SchemaError(err) => write!(f, "Schema error: {}", err),
35            DbError::TransactionError(msg) => write!(f, "Transaction error: {}", msg),
36            DbError::IndexError(msg) => write!(f, "Index error: {}", msg),
37            DbError::IoError(err) => write!(f, "IO error: {}", err),
38            DbError::InternalError(msg) => write!(f, "Internal error: {}", msg),
39            DbError::Other(msg) => write!(f, "{}", msg),
40        }
41    }
42}
43
44impl std::error::Error for DbError {}
45
46impl From<SchemaError> for DbError {
47    fn from(err: SchemaError) -> Self {
48        DbError::SchemaError(err)
49    }
50}
51
52impl From<std::io::Error> for DbError {
53    fn from(err: std::io::Error) -> Self {
54        DbError::IoError(err)
55    }
56}
57
58/// 数据库操作结果
59pub type DbResult<T> = Result<T, DbError>;