#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("open failed: {0}")]
Open(String),
#[error("connect failed: {0}")]
Connect(String),
#[error("query failed: {0}")]
Query(String),
#[error("sync failed: {0}")]
Sync(String),
#[error("sync failed: {0}() on a local-only database")]
NotSynced(&'static str),
#[error("database is busy: {0}")]
Busy(String),
#[error("type error: {0}")]
Type(String),
#[error("database is closed")]
Closed,
#[error("statement interrupted before it ran")]
Interrupted,
#[error("database opened in process {opened} used after fork in process {current}; open a new database in the child")]
Forked { opened: u32, current: u32 },
}
impl Error {
pub(crate) fn from_engine(kind: fn(String) -> Error, e: turso::Error) -> Error {
match e {
turso::Error::Busy(m) | turso::Error::BusySnapshot(m) => Error::Busy(m),
turso::Error::Error(m) if m.contains("Database is busy") => Error::Busy(m),
other => kind(other.to_string()),
}
}
}
pub type Result<T> = std::result::Result<T, Error>;
#[cfg(test)]
mod tests {
use super::Error;
#[test]
fn error_displays_context() {
let e = Error::Query("boom".into());
assert_eq!(e.to_string(), "query failed: boom");
}
}