1use axum::http::StatusCode;
2use axum::response::{IntoResponse, Response};
3
4#[derive(Debug)]
5pub enum RouteError {
6 DbError(kellnr_db::error::DbError),
7 InsufficientPrivileges,
8 Status(StatusCode),
9 AuthenticationFailure,
10 UserNotFound(String),
11}
12
13impl From<kellnr_db::error::DbError> for RouteError {
14 fn from(err: kellnr_db::error::DbError) -> Self {
15 match err {
16 kellnr_db::error::DbError::PasswordMismatch => Self::AuthenticationFailure,
17 kellnr_db::error::DbError::UserNotFound(name) => Self::UserNotFound(name),
18 _ => Self::DbError(err),
19 }
20 }
21}
22
23impl IntoResponse for RouteError {
24 fn into_response(self) -> Response {
25 match self {
26 Self::AuthenticationFailure => {
27 tracing::warn!("Login with wrong username or password");
28 StatusCode::UNAUTHORIZED.into_response()
29 }
30 RouteError::DbError(err) => {
31 tracing::error!("Db: {err:?}");
32 StatusCode::INTERNAL_SERVER_ERROR.into_response()
33 }
34 RouteError::Status(status) => status.into_response(),
35 RouteError::InsufficientPrivileges => StatusCode::FORBIDDEN.into_response(),
36 RouteError::UserNotFound(name) => {
37 tracing::warn!("User not found: {name}");
38 StatusCode::NOT_FOUND.into_response()
39 }
40 }
41 }
42}