1pub type Result<T> = std::result::Result<T, Error>;
35
36#[derive(Debug, thiserror::Error)]
53pub enum Error {
54 #[error("I/O error: {0}")]
56 Io(#[from] std::io::Error),
57
58 #[error("Storage error: {0}")]
60 Storage(String),
61
62 #[error("SQL parse error: {0}")]
64 SqlParse(String),
65
66 #[error("Query execution error: {0}")]
68 QueryExecution(String),
69
70 #[error("Query timeout: {0}")]
72 QueryTimeout(String),
73
74 #[error("Query cancelled: {0}")]
76 QueryCancelled(String),
77
78 #[error("Transaction error: {0}")]
80 Transaction(String),
81
82 #[error("Type conversion error: {0}")]
84 TypeConversion(String),
85
86 #[error("Invalid configuration: {0}")]
88 Config(String),
89
90 #[error("Encryption error: {0}")]
92 Encryption(String),
93
94 #[error("Protocol error: {0}")]
96 Protocol(String),
97
98 #[error("Vector index error: {0}")]
100 VectorIndex(String),
101
102 #[error("Multi-tenancy error: {0}")]
104 MultiTenant(String),
105
106 #[error("Audit error: {0}")]
108 Audit(String),
109
110 #[error("Compression error: {0}")]
112 Compression(String),
113
114 #[error("Branch merge error: {0}")]
116 BranchMerge(String),
117
118 #[error("Merge conflict: {0}")]
120 MergeConflict(String),
121
122 #[error("Constraint violation: {0}")]
124 ConstraintViolation(String),
125
126 #[error("Lock poisoning error: {0}")]
128 LockPoisoned(String),
129
130 #[error("{0}")]
132 Generic(String),
133}
134
135impl Error {
136 pub fn storage(msg: impl Into<String>) -> Self {
138 Error::Storage(msg.into())
139 }
140
141 pub fn sql_parse(msg: impl Into<String>) -> Self {
143 Error::SqlParse(msg.into())
144 }
145
146 pub fn query_execution(msg: impl Into<String>) -> Self {
148 Error::QueryExecution(msg.into())
149 }
150
151 pub fn query_timeout(msg: impl Into<String>) -> Self {
153 Error::QueryTimeout(msg.into())
154 }
155
156 pub fn query_cancelled(msg: impl Into<String>) -> Self {
158 Error::QueryCancelled(msg.into())
159 }
160
161 pub fn transaction(msg: impl Into<String>) -> Self {
163 Error::Transaction(msg.into())
164 }
165
166 pub fn type_conversion(msg: impl Into<String>) -> Self {
168 Error::TypeConversion(msg.into())
169 }
170
171 pub fn config(msg: impl Into<String>) -> Self {
173 Error::Config(msg.into())
174 }
175
176 pub fn encryption(msg: impl Into<String>) -> Self {
178 Error::Encryption(msg.into())
179 }
180
181 pub fn protocol(msg: impl Into<String>) -> Self {
183 Error::Protocol(msg.into())
184 }
185
186 pub fn vector_index(msg: impl Into<String>) -> Self {
188 Error::VectorIndex(msg.into())
189 }
190
191 pub fn multi_tenant(msg: impl Into<String>) -> Self {
193 Error::MultiTenant(msg.into())
194 }
195
196 pub fn audit(msg: impl Into<String>) -> Self {
198 Error::Audit(msg.into())
199 }
200
201 pub fn compression(msg: impl Into<String>) -> Self {
203 Error::Compression(msg.into())
204 }
205
206 pub fn branch_merge(msg: impl Into<String>) -> Self {
208 Error::BranchMerge(msg.into())
209 }
210
211 pub fn merge_conflict(msg: impl Into<String>) -> Self {
213 Error::MergeConflict(msg.into())
214 }
215
216 pub fn constraint_violation(msg: impl Into<String>) -> Self {
218 Error::ConstraintViolation(msg.into())
219 }
220
221 pub fn network(msg: impl Into<String>) -> Self {
223 Error::Protocol(msg.into())
224 }
225
226 pub fn authentication(msg: impl Into<String>) -> Self {
228 Error::Protocol(msg.into())
229 }
230
231 pub fn io(msg: impl Into<String>) -> Self {
233 Error::Io(std::io::Error::new(std::io::ErrorKind::Other, msg.into()))
234 }
235
236 pub fn internal(msg: impl Into<String>) -> Self {
238 Error::Generic(format!("Internal error: {}", msg.into()))
239 }
240
241 pub fn execution(msg: impl Into<String>) -> Self {
243 Error::QueryExecution(msg.into())
244 }
245
246 pub fn lock_poisoned(msg: impl Into<String>) -> Self {
248 Error::LockPoisoned(msg.into())
249 }
250
251 pub fn resource_limit(msg: impl Into<String>) -> Self {
253 Error::Generic(format!("Resource limit exceeded: {}", msg.into()))
254 }
255
256 pub fn deadlock(msg: impl Into<String>) -> Self {
258 Error::Transaction(format!("Deadlock: {}", msg.into()))
259 }
260
261 pub fn ha(msg: impl Into<String>) -> Self {
263 Error::Generic(format!("HA error: {}", msg.into()))
264 }
265
266 pub fn switchover(msg: impl Into<String>) -> Self {
268 Error::Generic(format!("Switchover error: {}", msg.into()))
269 }
270
271 pub fn replication(msg: impl Into<String>) -> Self {
273 Error::Generic(format!("Replication error: {}", msg.into()))
274 }
275}
276
277pub trait LockResultExt<T> {
279 fn map_lock_err(self, context: &str) -> Result<T>;
281}
282
283impl<T, E> LockResultExt<T> for std::result::Result<T, E>
284where
285 E: std::fmt::Display,
286{
287 fn map_lock_err(self, context: &str) -> Result<T> {
288 self.map_err(|e| Error::lock_poisoned(format!("{}: {}", context, e)))
289 }
290}
291
292impl From<rocksdb::Error> for Error {
294 fn from(err: rocksdb::Error) -> Self {
295 Error::Storage(err.to_string())
296 }
297}
298
299impl From<sqlparser::parser::ParserError> for Error {
300 fn from(err: sqlparser::parser::ParserError) -> Self {
301 Error::SqlParse(err.to_string())
302 }
303}