i18n+filtering fork - fluent-templates v2
at main 2.9 kB view raw
1use axum::response::{IntoResponse, Redirect, Response}; 2use http::StatusCode; 3use thiserror::Error; 4 5use crate::http::utils::stringify; 6 7/// Represents errors that can occur during web session operations. 8/// 9/// These errors are related to the serialization and deserialization of 10/// web session data used for maintaining user authentication state. 11#[derive(Debug, Error)] 12pub enum WebSessionError { 13 /// Error when web session deserialization fails. 14 /// 15 /// This error occurs when attempting to deserialize a web session from JSON 16 /// format, typically when retrieving a session from storage or a cookie. 17 #[error("error-websession-1 Unable to deserialize WebSession: {0:?}")] 18 DeserializeFailed(serde_json::Error), 19 20 /// Error when web session serialization fails. 21 /// 22 /// This error occurs when attempting to serialize a web session to JSON 23 /// format, typically when storing a session in storage or a cookie. 24 #[error("error-websession-2 Unable to serialize WebSession: {0:?}")] 25 SerializeFailed(serde_json::Error), 26} 27 28/// Represents errors that can occur during authentication middleware operations. 29/// 30/// These errors typically happen in the authentication middleware layer when 31/// processing requests, including cryptographic operations and session validation. 32#[derive(Debug, Error)] 33pub enum AuthMiddlewareError { 34 /// Error when content signing fails. 35 /// 36 /// This error occurs when the authentication middleware attempts to 37 /// cryptographically sign content but the operation fails. 38 #[error("error-authmiddleware-1 Unable to sign content: {0:?}")] 39 SigningFailed(p256::ecdsa::Error), 40} 41 42#[derive(Debug, Error)] 43pub enum MiddlewareAuthError { 44 #[error("error-middleware-auth-1 Access Denied: {0}")] 45 AccessDenied(String), 46 47 #[error("error-middleware-auth-2 Not Found")] 48 NotFound, 49 50 #[error("error-middleware-auth-3 Unhandled Auth Error: {0:?}")] 51 Anyhow(#[from] anyhow::Error), 52 53 #[error(transparent)] 54 AuthError(#[from] AuthMiddlewareError), 55} 56 57impl IntoResponse for MiddlewareAuthError { 58 fn into_response(self) -> Response { 59 match self { 60 MiddlewareAuthError::AccessDenied(destination) => { 61 let encoded_destination = urlencoding::encode(&destination).to_string(); 62 let args = vec![("destination", encoded_destination.as_str())]; 63 let uri = format!("/oauth/login?{}", stringify(args)); 64 Redirect::to(&uri).into_response() 65 } 66 MiddlewareAuthError::NotFound => { 67 tracing::error!(error = ?self, "access denied"); 68 (StatusCode::NOT_FOUND).into_response() 69 } 70 _ => { 71 tracing::error!(error = ?self, "internal server error"); 72 (StatusCode::INTERNAL_SERVER_ERROR).into_response() 73 } 74 } 75 } 76}