auth_framework/api/
error_codes.rs

1//! Standard API Error Codes
2//!
3//! Consistent error codes for API responses
4
5/// Standard error codes used across the API
6pub struct ErrorCodes;
7
8impl ErrorCodes {
9    // Authentication errors
10    pub const INVALID_CREDENTIALS: &'static str = "INVALID_CREDENTIALS";
11    pub const TOKEN_EXPIRED: &'static str = "TOKEN_EXPIRED";
12    pub const TOKEN_INVALID: &'static str = "TOKEN_INVALID";
13    pub const MFA_REQUIRED: &'static str = "MFA_REQUIRED";
14    pub const MFA_INVALID: &'static str = "MFA_INVALID";
15
16    // Authorization errors
17    pub const INSUFFICIENT_PERMISSIONS: &'static str = "INSUFFICIENT_PERMISSIONS";
18    pub const FORBIDDEN: &'static str = "FORBIDDEN";
19    pub const UNAUTHORIZED: &'static str = "UNAUTHORIZED";
20
21    // Validation errors
22    pub const VALIDATION_ERROR: &'static str = "VALIDATION_ERROR";
23    pub const INVALID_REQUEST: &'static str = "INVALID_REQUEST";
24    pub const MISSING_PARAMETER: &'static str = "MISSING_PARAMETER";
25
26    // Resource errors
27    pub const NOT_FOUND: &'static str = "NOT_FOUND";
28    pub const ALREADY_EXISTS: &'static str = "ALREADY_EXISTS";
29    pub const CONFLICT: &'static str = "CONFLICT";
30
31    // Rate limiting
32    pub const RATE_LIMITED: &'static str = "RATE_LIMITED";
33    pub const TOO_MANY_REQUESTS: &'static str = "TOO_MANY_REQUESTS";
34
35    // Server errors
36    pub const INTERNAL_ERROR: &'static str = "INTERNAL_ERROR";
37    pub const SERVICE_UNAVAILABLE: &'static str = "SERVICE_UNAVAILABLE";
38    pub const MAINTENANCE_MODE: &'static str = "MAINTENANCE_MODE";
39
40    // OAuth specific
41    pub const INVALID_GRANT: &'static str = "INVALID_GRANT";
42    pub const UNSUPPORTED_GRANT_TYPE: &'static str = "UNSUPPORTED_GRANT_TYPE";
43    pub const INVALID_CLIENT: &'static str = "INVALID_CLIENT";
44    pub const INVALID_SCOPE: &'static str = "INVALID_SCOPE";
45}
46
47/// Error code utility functions
48impl ErrorCodes {
49    /// Check if an error code is retryable
50    pub fn is_retryable(code: &str) -> bool {
51        matches!(
52            code,
53            Self::INTERNAL_ERROR
54                | Self::SERVICE_UNAVAILABLE
55                | Self::RATE_LIMITED
56                | Self::TOO_MANY_REQUESTS
57        )
58    }
59
60    /// Get human-readable description for error code
61    pub fn description(code: &str) -> &'static str {
62        match code {
63            Self::INVALID_CREDENTIALS => "The provided credentials are invalid",
64            Self::TOKEN_EXPIRED => "The authentication token has expired",
65            Self::TOKEN_INVALID => "The authentication token is invalid",
66            Self::MFA_REQUIRED => "Multi-factor authentication is required",
67            Self::MFA_INVALID => "The MFA code is invalid",
68            Self::INSUFFICIENT_PERMISSIONS => "Insufficient permissions for this operation",
69            Self::FORBIDDEN => "Access to this resource is forbidden",
70            Self::UNAUTHORIZED => "Authentication is required",
71            Self::VALIDATION_ERROR => "Request validation failed",
72            Self::INVALID_REQUEST => "The request is malformed or invalid",
73            Self::MISSING_PARAMETER => "A required parameter is missing",
74            Self::NOT_FOUND => "The requested resource was not found",
75            Self::ALREADY_EXISTS => "The resource already exists",
76            Self::CONFLICT => "The request conflicts with the current state",
77            Self::RATE_LIMITED => "Request rate limit exceeded",
78            Self::TOO_MANY_REQUESTS => "Too many requests in a short time",
79            Self::INTERNAL_ERROR => "An internal server error occurred",
80            Self::SERVICE_UNAVAILABLE => "The service is temporarily unavailable",
81            Self::MAINTENANCE_MODE => "The service is in maintenance mode",
82            Self::INVALID_GRANT => "The authorization grant is invalid",
83            Self::UNSUPPORTED_GRANT_TYPE => "The grant type is not supported",
84            Self::INVALID_CLIENT => "The client credentials are invalid",
85            Self::INVALID_SCOPE => "The requested scope is invalid",
86            _ => "Unknown error",
87        }
88    }
89}
90
91