pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
#[error("Storage error: {0}")]
Storage(String),
#[error("SQL parse error: {0}")]
SqlParse(String),
#[error("Query execution error: {0}")]
QueryExecution(String),
#[error("Query timeout: {0}")]
QueryTimeout(String),
#[error("Query cancelled: {0}")]
QueryCancelled(String),
#[error("Transaction error: {0}")]
Transaction(String),
#[error("Type conversion error: {0}")]
TypeConversion(String),
#[error("Invalid configuration: {0}")]
Config(String),
#[error("Encryption error: {0}")]
Encryption(String),
#[error("Protocol error: {0}")]
Protocol(String),
#[error("Vector index error: {0}")]
VectorIndex(String),
#[error("Multi-tenancy error: {0}")]
MultiTenant(String),
#[error("Audit error: {0}")]
Audit(String),
#[error("Compression error: {0}")]
Compression(String),
#[error("Branch merge error: {0}")]
BranchMerge(String),
#[error("Merge conflict: {0}")]
MergeConflict(String),
#[error("Constraint violation: {0}")]
ConstraintViolation(String),
#[error("Lock poisoning error: {0}")]
LockPoisoned(String),
#[error("{0}")]
Generic(String),
}
impl Error {
pub fn storage(msg: impl Into<String>) -> Self {
Error::Storage(msg.into())
}
pub fn sql_parse(msg: impl Into<String>) -> Self {
Error::SqlParse(msg.into())
}
pub fn query_execution(msg: impl Into<String>) -> Self {
Error::QueryExecution(msg.into())
}
pub fn query_timeout(msg: impl Into<String>) -> Self {
Error::QueryTimeout(msg.into())
}
pub fn query_cancelled(msg: impl Into<String>) -> Self {
Error::QueryCancelled(msg.into())
}
pub fn transaction(msg: impl Into<String>) -> Self {
Error::Transaction(msg.into())
}
pub fn type_conversion(msg: impl Into<String>) -> Self {
Error::TypeConversion(msg.into())
}
pub fn config(msg: impl Into<String>) -> Self {
Error::Config(msg.into())
}
pub fn encryption(msg: impl Into<String>) -> Self {
Error::Encryption(msg.into())
}
pub fn protocol(msg: impl Into<String>) -> Self {
Error::Protocol(msg.into())
}
pub fn vector_index(msg: impl Into<String>) -> Self {
Error::VectorIndex(msg.into())
}
pub fn multi_tenant(msg: impl Into<String>) -> Self {
Error::MultiTenant(msg.into())
}
pub fn audit(msg: impl Into<String>) -> Self {
Error::Audit(msg.into())
}
pub fn compression(msg: impl Into<String>) -> Self {
Error::Compression(msg.into())
}
pub fn branch_merge(msg: impl Into<String>) -> Self {
Error::BranchMerge(msg.into())
}
pub fn merge_conflict(msg: impl Into<String>) -> Self {
Error::MergeConflict(msg.into())
}
pub fn constraint_violation(msg: impl Into<String>) -> Self {
Error::ConstraintViolation(msg.into())
}
pub fn network(msg: impl Into<String>) -> Self {
Error::Protocol(msg.into())
}
pub fn authentication(msg: impl Into<String>) -> Self {
Error::Protocol(msg.into())
}
pub fn io(msg: impl Into<String>) -> Self {
Error::Io(std::io::Error::new(std::io::ErrorKind::Other, msg.into()))
}
pub fn internal(msg: impl Into<String>) -> Self {
Error::Generic(format!("Internal error: {}", msg.into()))
}
pub fn execution(msg: impl Into<String>) -> Self {
Error::QueryExecution(msg.into())
}
pub fn lock_poisoned(msg: impl Into<String>) -> Self {
Error::LockPoisoned(msg.into())
}
pub fn resource_limit(msg: impl Into<String>) -> Self {
Error::Generic(format!("Resource limit exceeded: {}", msg.into()))
}
pub fn deadlock(msg: impl Into<String>) -> Self {
Error::Transaction(format!("Deadlock: {}", msg.into()))
}
pub fn ha(msg: impl Into<String>) -> Self {
Error::Generic(format!("HA error: {}", msg.into()))
}
pub fn switchover(msg: impl Into<String>) -> Self {
Error::Generic(format!("Switchover error: {}", msg.into()))
}
pub fn replication(msg: impl Into<String>) -> Self {
Error::Generic(format!("Replication error: {}", msg.into()))
}
}
pub trait LockResultExt<T> {
fn map_lock_err(self, context: &str) -> Result<T>;
}
impl<T, E> LockResultExt<T> for std::result::Result<T, E>
where
E: std::fmt::Display,
{
fn map_lock_err(self, context: &str) -> Result<T> {
self.map_err(|e| Error::lock_poisoned(format!("{}: {}", context, e)))
}
}
impl From<rocksdb::Error> for Error {
fn from(err: rocksdb::Error) -> Self {
Error::Storage(err.to_string())
}
}
impl From<sqlparser::parser::ParserError> for Error {
fn from(err: sqlparser::parser::ParserError) -> Self {
Error::SqlParse(err.to_string())
}
}