use thiserror::Error;
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum Error {
#[error("SQLite error: {0}")]
Sqlite(#[from] rusqlite::Error),
#[error("Object store error: {0}")]
ObjectStore(#[from] object_store::Error),
#[error("WAL parse error at offset {offset}: {detail}")]
WalParse { offset: u64, detail: String },
#[error("WAL checksum mismatch at frame {frame}: ck1 stored {stored_1:#x}, computed {computed_1:#x}; ck2 stored {stored_2:#x}, computed {computed_2:#x}")]
WalChecksum {
frame: u32,
stored_1: u32,
computed_1: u32,
stored_2: u32,
computed_2: u32,
},
#[error("Restore error: {0}")]
Restore(String),
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("Replication error: {0}")]
Replication(String),
#[error("Manifest conflict: remote is ahead (gen {remote_gen} vs local {local_gen}). Re-restore required.")]
ManifestConflict { local_gen: u64, remote_gen: u64 },
#[error("Snapshot error: {0}")]
Snapshot(String),
#[error("Configuration error: {0}")]
Config(String),
#[error("Serialization error: {0}")]
Serialization(#[from] serde_json::Error),
#[error("Task join error: {0}")]
Join(#[from] tokio::task::JoinError),
#[error("{0}")]
Other(String),
#[error(
"Precondition failed for key '{key}': expected etag '{expected}', object was modified"
)]
PreconditionFailed { key: String, expected: String },
#[error("Compaction error: {0}")]
Compaction(String),
#[error("Database locked by another process: {path} (PID {holder_pid})")]
DatabaseLocked {
path: std::path::PathBuf,
holder_pid: u32,
},
#[error("Replica locked by another process: prefix '{prefix}' held by PID {holder_pid} on {holder_hostname} (last heartbeat {last_heartbeat})")]
ReplicaLocked {
prefix: String,
holder_pid: u32,
holder_hostname: String,
last_heartbeat: String,
},
#[error("THIS PREFIX BELONGS TO ANOTHER SERVER: prefix '{prefix}' is owned by '{expected_writer}', you are '{actual_writer}'. Do NOT share bucket prefixes between servers. Each server gets its own prefix.")]
WrongWriter {
prefix: String,
expected_writer: String,
actual_writer: String,
},
}
impl Error {
pub fn is_not_found(&self) -> bool {
match self {
Error::ObjectStore(e) => matches!(e, object_store::Error::NotFound { .. }),
_ => false,
}
}
pub fn is_io_not_found(&self) -> bool {
match self {
Error::Io(e) => e.kind() == std::io::ErrorKind::NotFound,
_ => false,
}
}
pub fn is_retryable(&self) -> bool {
match self {
Error::ObjectStore(e) => !matches!(e, object_store::Error::NotFound { .. }),
Error::Io(e) => matches!(
e.kind(),
std::io::ErrorKind::Interrupted
| std::io::ErrorKind::WouldBlock
| std::io::ErrorKind::ConnectionRefused
| std::io::ErrorKind::ConnectionReset
| std::io::ErrorKind::ConnectionAborted
| std::io::ErrorKind::TimedOut
| std::io::ErrorKind::NotConnected
),
Error::PreconditionFailed { .. } => false,
Error::ManifestConflict { .. } => false,
Error::Config(_) => false,
Error::WalParse { .. } => false,
Error::WalChecksum { .. } => false,
Error::DatabaseLocked { .. } => false,
Error::ReplicaLocked { .. } => false,
Error::WrongWriter { .. } => false,
Error::Sqlite(_) => true,
Error::Restore(_) => false,
Error::Replication(_) => true,
Error::Snapshot(_) => true,
Error::Serialization(_) => false,
Error::Join(_) => false,
Error::Other(_) => false,
Error::Compaction(_) => true,
}
}
pub fn is_database_locked(&self) -> bool {
matches!(self, Error::DatabaseLocked { .. })
}
pub fn is_replica_locked(&self) -> bool {
matches!(self, Error::ReplicaLocked { .. })
}
pub fn is_wrong_writer(&self) -> bool {
matches!(self, Error::WrongWriter { .. })
}
pub fn is_precondition_failed(&self) -> bool {
matches!(self, Error::PreconditionFailed { .. })
}
}
pub type Result<T> = std::result::Result<T, Error>;