Skip to main content

server_common/error/
auth.rs

1use axum::{
2    http::StatusCode,
3    response::{IntoResponse, Response},
4};
5
6// 定义认证错误类型
7#[derive(Debug)]
8pub enum AuthError {
9    InvalidToken,
10    MissingToken,
11    ExpiredToken,
12    InsufficientPermissions,
13}
14
15// 为 AuthError 实现 IntoResponse
16impl IntoResponse for AuthError {
17    fn into_response(self) -> Response {
18        let (status, error_message) = match self {
19            AuthError::InvalidToken => (StatusCode::UNAUTHORIZED, "Invalid token"),
20            AuthError::MissingToken => (StatusCode::UNAUTHORIZED, "Missing authentication token"),
21            AuthError::ExpiredToken => (StatusCode::UNAUTHORIZED, "Token has expired"),
22            AuthError::InsufficientPermissions => {
23                (StatusCode::FORBIDDEN, "Insufficient permissions")
24            }
25        };
26
27        (status, error_message).into_response()
28    }
29}