The smokesignal.events web application
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(crate) 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-smokesignal-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-smokesignal-websession-2 Unable to serialize WebSession: {0:?}")]
25 SerializeFailed(serde_json::Error),
26}
27
28#[derive(Debug, Error)]
29pub(crate) enum MiddlewareAuthError {
30 #[error("error-smokesignal-middleware-auth-1 Access Denied: {0}")]
31 AccessDenied(String),
32
33 #[error("error-smokesignal-middleware-auth-2 Not Found")]
34 NotFound,
35
36 #[error("error-smokesignal-middleware-auth-3 Unhandled Auth Error: {0:?}")]
37 Anyhow(#[from] anyhow::Error),
38}
39
40impl IntoResponse for MiddlewareAuthError {
41 fn into_response(self) -> Response {
42 match self {
43 MiddlewareAuthError::AccessDenied(destination) => {
44 let encoded_destination = urlencoding::encode(&destination).to_string();
45 let args = vec![("destination", encoded_destination.as_str())];
46 let uri = format!("/oauth/login?{}", stringify(args));
47 Redirect::to(&uri).into_response()
48 }
49 MiddlewareAuthError::NotFound => {
50 tracing::error!(error = ?self, "access denied");
51 (StatusCode::NOT_FOUND).into_response()
52 }
53 _ => {
54 tracing::error!(error = ?self, "internal server error");
55 (StatusCode::INTERNAL_SERVER_ERROR).into_response()
56 }
57 }
58 }
59}