use crate::errors::{ErrorSeverity, UserFriendlyError};
use thiserror::Error;
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum AuthzError {
#[error("Permission collision: {collision_count} permissions map to hash {hash_id}")]
PermissionCollision {
collision_count: usize,
hash_id: u64,
permissions: Vec<String>,
},
}
impl AuthzError {
pub fn collision(hash_id: u64, permissions: Vec<String>) -> Self {
AuthzError::PermissionCollision {
collision_count: permissions.len(),
hash_id,
permissions,
}
}
fn support_code_inner(&self) -> String {
match self {
AuthzError::PermissionCollision { hash_id, .. } => {
format!("AUTHZ-PERM-COLLISION-{}", hash_id)
}
}
}
}
impl UserFriendlyError for AuthzError {
fn user_message(&self) -> String {
match self {
AuthzError::PermissionCollision { .. } => {
"There's a technical issue with your account permissions. Our support team has been notified and will resolve this shortly. Please contact support if you need immediate assistance.".to_string()
}
}
}
fn developer_message(&self) -> String {
match self {
AuthzError::PermissionCollision {
collision_count,
hash_id,
permissions,
} => {
format!(
"Permission collision detected: {} permissions [{}] map to hash ID {}. This indicates a critical hash collision in the permission system requiring immediate administrator attention.",
collision_count,
permissions.join(", "),
hash_id
)
}
}
}
fn support_code(&self) -> String {
self.support_code_inner()
}
fn severity(&self) -> ErrorSeverity {
match self {
AuthzError::PermissionCollision { .. } => ErrorSeverity::Critical,
}
}
fn suggested_actions(&self) -> Vec<String> {
match self {
AuthzError::PermissionCollision { .. } => vec![
"Contact our support team immediately with the reference code below".to_string(),
"Do not attempt to retry this operation".to_string(),
"This is a critical system issue requiring immediate administrator attention"
.to_string(),
],
}
}
fn is_retryable(&self) -> bool {
match self {
AuthzError::PermissionCollision { .. } => false, }
}
}