1use std::fmt;
23use thiserror::Error;
24
25pub type KernelResult<T> = Result<T, KernelError>;
27
28#[derive(Error, Debug)]
30pub enum KernelError {
31 #[error("I/O error: {0}")]
33 Io(#[from] std::io::Error),
34
35 #[error("Transaction error: {kind}")]
37 Transaction { kind: TransactionErrorKind },
38
39 #[error("WAL error: {kind}")]
41 Wal { kind: WalErrorKind },
42
43 #[error("Page error: {kind}")]
45 Page { kind: PageErrorKind },
46
47 #[error("Catalog error: {kind}")]
49 Catalog { kind: CatalogErrorKind },
50
51 #[error("Plugin error: {message}")]
53 Plugin { message: String },
54
55 #[error("Data corruption detected: {details}")]
57 Corruption { details: String },
58
59 #[error("Recovery error: {details}")]
61 Recovery { details: String },
62}
63
64#[derive(Debug, Clone, PartialEq, Eq)]
66pub enum TransactionErrorKind {
67 NotFound(u64),
69 AlreadyCommitted,
71 AlreadyAborted,
73 WriteWriteConflict { row_id: u64 },
75 SerializationFailure,
77 Deadlock,
79 Timeout,
81}
82
83impl fmt::Display for TransactionErrorKind {
84 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
85 match self {
86 Self::NotFound(id) => write!(f, "transaction {} not found", id),
87 Self::AlreadyCommitted => write!(f, "transaction already committed"),
88 Self::AlreadyAborted => write!(f, "transaction already aborted"),
89 Self::WriteWriteConflict { row_id } => {
90 write!(f, "write-write conflict on row {}", row_id)
91 }
92 Self::SerializationFailure => write!(f, "serialization failure"),
93 Self::Deadlock => write!(f, "deadlock detected"),
94 Self::Timeout => write!(f, "transaction timeout"),
95 }
96 }
97}
98
99#[derive(Debug, Clone, PartialEq, Eq)]
101pub enum WalErrorKind {
102 InvalidLsn(u64),
104 ChecksumMismatch { expected: u32, actual: u32 },
106 Corrupted,
108 Full,
110 FsyncFailed,
112}
113
114impl fmt::Display for WalErrorKind {
115 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
116 match self {
117 Self::InvalidLsn(lsn) => write!(f, "invalid LSN: {}", lsn),
118 Self::ChecksumMismatch { expected, actual } => {
119 write!(
120 f,
121 "checksum mismatch: expected {}, got {}",
122 expected, actual
123 )
124 }
125 Self::Corrupted => write!(f, "WAL corrupted"),
126 Self::Full => write!(f, "WAL full"),
127 Self::FsyncFailed => write!(f, "fsync failed"),
128 }
129 }
130}
131
132#[derive(Debug, Clone, PartialEq, Eq)]
134pub enum PageErrorKind {
135 NotFound(u64),
137 Corrupted(u64),
139 BufferPoolFull,
141 InvalidSize,
143}
144
145impl fmt::Display for PageErrorKind {
146 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
147 match self {
148 Self::NotFound(id) => write!(f, "page {} not found", id),
149 Self::Corrupted(id) => write!(f, "page {} corrupted", id),
150 Self::BufferPoolFull => write!(f, "buffer pool full"),
151 Self::InvalidSize => write!(f, "invalid page size"),
152 }
153 }
154}
155
156#[derive(Debug, Clone, PartialEq, Eq)]
158pub enum CatalogErrorKind {
159 TableNotFound(String),
161 TableExists(String),
163 ColumnNotFound(String),
165 SchemaMismatch,
167}
168
169impl fmt::Display for CatalogErrorKind {
170 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
171 match self {
172 Self::TableNotFound(name) => write!(f, "table '{}' not found", name),
173 Self::TableExists(name) => write!(f, "table '{}' already exists", name),
174 Self::ColumnNotFound(name) => write!(f, "column '{}' not found", name),
175 Self::SchemaMismatch => write!(f, "schema mismatch"),
176 }
177 }
178}