Skip to main content

allowthem_server/
error.rs

1use axum::http::StatusCode;
2use axum::response::{IntoResponse, Response};
3use serde_json::json;
4
5use allowthem_core::AuthError;
6
7/// Error type for authentication extractor failures.
8///
9/// Implements `IntoResponse` to produce appropriate HTTP error responses.
10/// Used as the `Rejection` type for [`AuthUser`](crate::AuthUser).
11#[derive(Debug)]
12pub enum AuthExtractError {
13    /// No valid session. Covers: missing cookie, invalid token, expired
14    /// session, orphaned session (user deleted), or inactive user.
15    Unauthenticated,
16    /// Database or internal error during extraction.
17    Internal(AuthError),
18}
19
20impl IntoResponse for AuthExtractError {
21    fn into_response(self) -> Response {
22        match self {
23            Self::Unauthenticated => (
24                StatusCode::UNAUTHORIZED,
25                axum::Json(json!({"error": "unauthenticated"})),
26            )
27                .into_response(),
28            Self::Internal(err) => {
29                tracing::error!("auth extraction error: {err}");
30                (
31                    StatusCode::INTERNAL_SERVER_ERROR,
32                    axum::Json(json!({"error": "internal error"})),
33                )
34                    .into_response()
35            }
36        }
37    }
38}