use thiserror::Error;
#[derive(Debug, Error)]
pub enum GuardedError {
#[error("guarded: predicate matched no rows")]
NoRowsAffected,
#[error(
"guarded: predicate matched {affected} rows (expected 1) — likely an index/uniqueness bug"
)]
TooManyRows { affected: u64 },
#[error("guarded: no columns to set — builder is empty")]
EmptyUpdate,
#[error("guarded: db error: {0}")]
Db(#[from] sea_orm::DbErr),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn error_no_rows_affected_displays_message() {
assert_eq!(
GuardedError::NoRowsAffected.to_string(),
"guarded: predicate matched no rows"
);
}
#[test]
fn error_too_many_rows_displays_message() {
assert_eq!(
GuardedError::TooManyRows { affected: 3 }.to_string(),
"guarded: predicate matched 3 rows (expected 1) — likely an index/uniqueness bug"
);
}
#[test]
fn error_empty_update_displays_message() {
assert_eq!(
GuardedError::EmptyUpdate.to_string(),
"guarded: no columns to set — builder is empty"
);
}
#[test]
fn db_from_sea_orm_dberr() {
let db_err = sea_orm::DbErr::Custom("test".into());
let guarded_err: GuardedError = GuardedError::from(db_err);
assert!(matches!(guarded_err, GuardedError::Db(_)));
}
}