use axum::Json; use axum::http::StatusCode; use axum::response::{IntoResponse, Response}; use serde_json::json; #[derive(Debug, Clone)] pub enum SessionError { Unauthorized, Expired, Verifier(String), } impl std::fmt::Display for SessionError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Self::Unauthorized => write!(f, "unauthenticated"), Self::Expired => write!(f, "token expired"), Self::Verifier(message) => write!(f, "token rejected: {message}"), } } } impl std::error::Error for SessionError {} impl SessionError { pub fn code(&self) -> &'static str { match self { Self::Unauthorized => "unauthenticated", Self::Expired => "token_expired", Self::Verifier(_) => "invalid_token", } } } impl IntoResponse for SessionError { fn into_response(self) -> Response { let message = self.code(); ( StatusCode::UNAUTHORIZED, Json(json!({ "code": 401, "message": message, "data": null, "error": null, "error_code": message, "request_id": null, "log_id": null, })), ) .into_response() } }