vibesql_storage/
error.rs

1// ============================================================================
2// Errors
3// ============================================================================
4
5/// Result type for storage operations
6pub type StorageResult<T> = Result<T, StorageError>;
7
8#[derive(Debug, Clone, PartialEq)]
9pub enum StorageError {
10    TableNotFound(String),
11    ColumnCountMismatch {
12        expected: usize,
13        actual: usize,
14    },
15    ColumnIndexOutOfBounds {
16        index: usize,
17    },
18    CatalogError(String),
19    TransactionError(String),
20    RowNotFound,
21    IndexAlreadyExists(String),
22    IndexNotFound(String),
23    ColumnNotFound {
24        column_name: String,
25        table_name: String,
26    },
27    NullConstraintViolation {
28        column: String,
29    },
30    TypeMismatch {
31        column: String,
32        expected: String,
33        actual: String,
34    },
35    UniqueConstraintViolation(String),
36    InvalidIndexColumn(String),
37    NotImplemented(String),
38    IoError(String),
39    InvalidPageSize {
40        expected: usize,
41        actual: usize,
42    },
43    InvalidPageId(u64),
44    LockError(String),
45    MemoryBudgetExceeded {
46        used: usize,
47        budget: usize,
48    },
49    NoIndexToEvict,
50    /// Generic error for other cases
51    Other(String),
52}
53
54impl std::fmt::Display for StorageError {
55    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56        match self {
57            StorageError::TableNotFound(name) => write!(f, "Table '{}' not found", name),
58            StorageError::ColumnCountMismatch { expected, actual } => {
59                write!(f, "Column count mismatch: expected {}, got {}", expected, actual)
60            }
61            StorageError::ColumnIndexOutOfBounds { index } => {
62                write!(f, "Column index {} out of bounds", index)
63            }
64            StorageError::CatalogError(msg) => write!(f, "Catalog error: {}", msg),
65            StorageError::TransactionError(msg) => write!(f, "Transaction error: {}", msg),
66            StorageError::RowNotFound => write!(f, "Row not found"),
67            StorageError::IndexAlreadyExists(name) => write!(f, "Index '{}' already exists", name),
68            StorageError::IndexNotFound(name) => write!(f, "Index '{}' not found", name),
69            StorageError::ColumnNotFound { column_name, table_name } => {
70                write!(f, "Column '{}' not found in table '{}'", column_name, table_name)
71            }
72            StorageError::NullConstraintViolation { column } => {
73                write!(f, "NOT NULL constraint violation: column '{}' cannot be NULL", column)
74            }
75            StorageError::TypeMismatch { column, expected, actual } => {
76                write!(
77                    f,
78                    "Type mismatch in column '{}': expected {}, got {}",
79                    column, expected, actual
80                )
81            }
82            StorageError::UniqueConstraintViolation(msg) => write!(f, "{}", msg),
83            StorageError::InvalidIndexColumn(msg) => write!(f, "{}", msg),
84            StorageError::NotImplemented(msg) => write!(f, "Not implemented: {}", msg),
85            StorageError::IoError(msg) => write!(f, "I/O error: {}", msg),
86            StorageError::InvalidPageSize { expected, actual } => {
87                write!(f, "Invalid page size: expected {}, got {}", expected, actual)
88            }
89            StorageError::InvalidPageId(page_id) => write!(f, "Invalid page ID: {}", page_id),
90            StorageError::LockError(msg) => write!(f, "Lock error: {}", msg),
91            StorageError::MemoryBudgetExceeded { used, budget } => {
92                write!(
93                    f,
94                    "Memory budget exceeded: using {} bytes, budget is {} bytes",
95                    used, budget
96                )
97            }
98            StorageError::NoIndexToEvict => {
99                write!(f, "No index available to evict (all indexes are already disk-backed)")
100            }
101            StorageError::Other(msg) => write!(f, "{}", msg),
102        }
103    }
104}
105
106impl std::error::Error for StorageError {}