use turso_orm_driver::{ConstraintKind, Error as DriverError, ErrorKind};
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum DbErr {
#[error(transparent)]
Driver(#[from] DriverError),
#[error("record not found: {0}")]
RecordNotFound(String),
#[error("record not inserted")]
RecordNotInserted,
#[error("record not updated")]
RecordNotUpdated,
#[error("attribute not set: {0}")]
AttrNotSet(String),
#[error("primary key not set")]
PrimaryKeyNotSet,
#[error("type error: {0}")]
Type(String),
#[error("json error: {0}")]
Json(String),
#[error("migration error: {0}")]
Migration(String),
#[error("{0}")]
Custom(String),
}
impl DbErr {
pub fn kind(&self) -> Option<ErrorKind> {
match self {
DbErr::Driver(e) => Some(e.kind()),
_ => None,
}
}
pub fn constraint(&self) -> Option<ConstraintKind> {
match self {
DbErr::Driver(e) => e.constraint(),
_ => None,
}
}
pub fn is_busy(&self) -> bool {
matches!(self, DbErr::Driver(e) if e.is_busy())
}
pub fn is_unique_violation(&self) -> bool {
self.constraint() == Some(ConstraintKind::Unique)
}
pub fn is_foreign_key_violation(&self) -> bool {
self.constraint() == Some(ConstraintKind::ForeignKey)
}
}
pub type Result<T, E = DbErr> = std::result::Result<T, E>;