Skip to main content

allowthem_server/
browser_error.rs

1use axum::http::StatusCode;
2use axum::response::{IntoResponse, Response};
3
4#[derive(Debug)]
5pub enum BrowserError {
6    Template(minijinja::Error),
7    Auth(allowthem_core::AuthError),
8}
9
10impl From<minijinja::Error> for BrowserError {
11    fn from(err: minijinja::Error) -> Self {
12        BrowserError::Template(err)
13    }
14}
15
16impl From<allowthem_core::AuthError> for BrowserError {
17    fn from(err: allowthem_core::AuthError) -> Self {
18        BrowserError::Auth(err)
19    }
20}
21
22impl IntoResponse for BrowserError {
23    fn into_response(self) -> Response {
24        match self {
25            BrowserError::Template(e) => {
26                tracing::error!(error = %e, "template render failed");
27                StatusCode::INTERNAL_SERVER_ERROR.into_response()
28            }
29            BrowserError::Auth(allowthem_core::AuthError::NotFound) => {
30                StatusCode::NOT_FOUND.into_response()
31            }
32            BrowserError::Auth(allowthem_core::AuthError::Validation(msg)) => {
33                tracing::warn!(error = %msg, "validation error");
34                (StatusCode::UNPROCESSABLE_ENTITY, msg).into_response()
35            }
36            BrowserError::Auth(e) => {
37                tracing::error!(error = %e, "auth error");
38                StatusCode::INTERNAL_SERVER_ERROR.into_response()
39            }
40        }
41    }
42}