1use thiserror::Error;
4
5#[derive(Debug, Clone, Error)]
7#[error("Access denied: user '{user}' cannot access {permission}")]
8pub struct AccessDenied {
9 pub user: String,
11 pub permission: String,
13}
14
15impl AccessDenied {
16 pub fn new(user: impl Into<String>, permission: impl Into<String>) -> Self {
18 Self { user: user.into(), permission: permission.into() }
19 }
20}
21
22#[derive(Debug, Error)]
24pub enum AuthError {
25 #[error(transparent)]
27 AccessDenied(#[from] AccessDenied),
28
29 #[error("Role not found: {0}")]
31 RoleNotFound(String),
32
33 #[error("User not found: {0}")]
35 UserNotFound(String),
36
37 #[error("Audit error: {0}")]
39 AuditError(String),
40
41 #[error("IO error: {0}")]
43 IoError(#[from] std::io::Error),
44}
45
46impl From<AuthError> for adk_core::AdkError {
47 fn from(err: AuthError) -> Self {
48 use adk_core::{ErrorCategory, ErrorComponent};
49 let (category, code) = match &err {
50 AuthError::AccessDenied(_) => (ErrorCategory::Forbidden, "auth.access_denied"),
51 AuthError::RoleNotFound(_) => (ErrorCategory::NotFound, "auth.role_not_found"),
52 AuthError::UserNotFound(_) => (ErrorCategory::NotFound, "auth.user_not_found"),
53 AuthError::AuditError(_) => (ErrorCategory::Internal, "auth.audit"),
54 AuthError::IoError(_) => (ErrorCategory::Internal, "auth.io"),
55 };
56 adk_core::AdkError::new(ErrorComponent::Auth, category, code, err.to_string())
57 .with_source(err)
58 }
59}