use uuid::Uuid;
#[derive(thiserror::Error, Debug)]
pub enum ScopeError {
#[error("database error: {0}")]
Db(#[from] sea_orm::DbErr),
#[error("invalid scope: {0}")]
Invalid(&'static str),
#[error("access denied: tenant_id not present in security scope ({tenant_id})")]
TenantNotInScope { tenant_id: Uuid },
#[error("access denied: {0}")]
Denied(&'static str),
}
impl ScopeError {
#[must_use]
pub fn is_unique_violation(&self) -> bool {
match self {
Self::Db(db_err) => is_unique_violation(db_err),
_ => false,
}
}
}
#[must_use]
pub fn is_unique_violation(err: &sea_orm::DbErr) -> bool {
if matches!(
err.sql_err(),
Some(sea_orm::SqlErr::UniqueConstraintViolation(_))
) {
return true;
}
let msg = err.to_string().to_lowercase();
msg.contains("unique constraint")
|| msg.contains("duplicate key")
|| msg.contains("unique_violation")
|| msg.contains("duplicate entry")
|| msg.contains("unique constraint failed")
}