From d70edcc8967648c2019df2514ab7838a3b4b6b46 Mon Sep 17 00:00:00 2001 From: Rogier Reerink Date: Sat, 10 Oct 2026 17:49:21 +0200 Subject: [PATCH] This maybe a step in the right direction. --- server/src/error.rs | 230 ++++++++++---------- server/src/routes/extractors/auth/bearer.rs | 9 - server/src/routes/extractors/auth/header.rs | 12 - 3 files changed, 117 insertions(+), 134 deletions(-) diff --git a/server/src/error.rs b/server/src/error.rs index 4971482..dd168e6 100644 --- a/server/src/error.rs +++ b/server/src/error.rs @@ -1,3 +1,5 @@ +use std::borrow::Cow; + use axum::{ http::StatusCode, response::{IntoResponse, Response}, @@ -5,8 +7,8 @@ use axum::{ use crate::{ domain::DomainError, - repository::{ReferenceRelation, ReferenceViolationKind, RepositoryError}, - routes::extractors::auth::AuthError, + repository::RepositoryError, + routes::extractors::auth::{AuthError, AuthHeaderError, BearerError}, }; #[derive(Debug, thiserror::Error)] @@ -38,115 +40,121 @@ pub enum AppError { impl IntoResponse for AppError { fn into_response(self) -> Response { - match self { - Self::BadRequest(error) => { - tracing::debug!(%error, "bad request"); - return error_response(StatusCode::BAD_REQUEST, error); - } - Self::Conflict(error) => { - tracing::warn!(%error, "conflict"); - return error_response(StatusCode::CONFLICT, error); - } - Self::NotFound => { - tracing::debug!("not found"); - return error_response(StatusCode::NOT_FOUND, "Not found"); - } - Self::Unauthorized(message) => { - tracing::debug!(?message, "unauthorized"); - return error_response(StatusCode::UNAUTHORIZED, message); - } - Self::Authorization(AuthError::UnsupportedScheme) => { - tracing::debug!("authorization error: unsupported scheme"); - return error_response(StatusCode::UNAUTHORIZED, "Unsupported scheme"); - } - Self::Authorization(AuthError::Header(error)) => { - tracing::debug!(%error, "authorization header error"); - return error_response(StatusCode::UNAUTHORIZED, error.client_error_message()); - } - Self::Authorization(AuthError::Bearer(error)) => { - tracing::debug!(%error, "bearer token error"); - return error_response(StatusCode::UNAUTHORIZED, error.client_error_message()); - } - Self::Authorization(AuthError::Repository(error)) => { - tracing::debug!(%error, "repository error"); - } - Self::Domain(error) => { - tracing::debug!(%error, "domain error"); - return error_response(StatusCode::UNPROCESSABLE_ENTITY, error.to_string()); - } - Self::Repository(RepositoryError::Database(error)) => { - tracing::error!(%error, "database query failed"); - } - Self::Repository(RepositoryError::Decode(error)) => { - tracing::error!(%error, "invalid stored data"); - } - Self::Repository(RepositoryError::ReferenceViolation { - relation: ReferenceRelation::User, - kind: ReferenceViolationKind::Missing, - .. - }) => { - tracing::debug!("user not found (reference violation)"); - return error_response(StatusCode::NOT_FOUND, "User cannot be found"); - } - Self::Repository(RepositoryError::ReferenceViolation { - relation: ReferenceRelation::UserApp, - kind: ReferenceViolationKind::Missing, - .. - }) => { - tracing::debug!("user app not found (reference violation)"); - return error_response(StatusCode::NOT_FOUND, "User app cannot be found"); - } - Self::Repository(RepositoryError::ReferenceViolation { - relation: ReferenceRelation::Account, - kind: ReferenceViolationKind::Missing, - .. - }) => { - tracing::debug!("account not found (reference violation)"); - return error_response(StatusCode::UNPROCESSABLE_ENTITY, "Account cannot be found"); - } - Self::Repository(RepositoryError::ReferenceViolation { - relation: ReferenceRelation::Transaction, - kind: ReferenceViolationKind::StillReferenced, - .. - }) => { - tracing::debug!( - "still referenced by one or more transactions (reference violation)" - ); - return error_response( - StatusCode::CONFLICT, - "Still referenced by one or more transactions", - ); - } - Self::Repository(RepositoryError::ReferenceViolation { - relation: ReferenceRelation::RecurringTransaction, - kind: ReferenceViolationKind::StillReferenced, - .. - }) => { - tracing::debug!( - "still referenced by one or more recurring transactions (reference violation)" - ); - return error_response( - StatusCode::CONFLICT, - "Still referenced by one or more recurring transactions", - ); - } - Self::Repository(RepositoryError::ReferenceViolation { - relation, - kind, - source, - }) => { - tracing::error!(%relation, %kind, error = ?source, "unhandled reference violation"); - } - Self::Repository(RepositoryError::QueryConversion(error)) => { - tracing::debug!(%error, "query conversion error"); - return error_response(StatusCode::BAD_REQUEST, error.to_string()); - } - Self::Internal => { - tracing::error!("internal server error"); - } + let (status, message) = self.client_response(); + + if status.is_server_error() { + tracing::error!(error = ?self, %status, "request failed"); + } else if matches!(self, Self::Conflict(_)) { + tracing::warn!(error = ?self, %status, "request rejected"); + } else { + tracing::debug!(error = ?self, %status, "request rejected"); } - error_response(StatusCode::INTERNAL_SERVER_ERROR, "Internal server error") + error_response(status, message) + } +} + +trait ClientError { + fn client_response(&self) -> (StatusCode, Cow<'_, str>); +} + +fn internal_error() -> (StatusCode, Cow<'static, str>) { + ( + StatusCode::INTERNAL_SERVER_ERROR, + "Internal server error".into(), + ) +} + +impl ClientError for AppError { + fn client_response(&self) -> (StatusCode, Cow<'_, str>) { + match self { + Self::BadRequest(message) => (StatusCode::BAD_REQUEST, message.as_str().into()), + Self::Unauthorized(message) => (StatusCode::UNAUTHORIZED, message.as_str().into()), + Self::NotFound => (StatusCode::NOT_FOUND, "Not found".into()), + Self::Conflict(message) => (StatusCode::CONFLICT, message.as_str().into()), + Self::Authorization(error) => error.client_response(), + Self::Domain(error) => error.client_response(), + Self::Repository(error) => error.client_response(), + Self::Internal => internal_error(), + } + } +} + +impl ClientError for AuthError { + fn client_response(&self) -> (StatusCode, Cow<'_, str>) { + match self { + Self::UnsupportedScheme => (StatusCode::UNAUTHORIZED, "Unsupported scheme".into()), + Self::Bearer(error) => error.client_response(), + Self::Header(error) => error.client_response(), + Self::Repository(error) => error.client_response(), + } + } +} + +impl ClientError for AuthHeaderError { + fn client_response(&self) -> (StatusCode, Cow<'_, str>) { + let message = match self { + Self::Missing => "Missing authorization header", + Self::NotAscii(_) => "Invalid authorization header encoding", + Self::MalformedValue => "Malformed authorization header", + Self::EmptyScheme => "Empty authorization scheme", + Self::EmptyToken => "Empty authorization token", + }; + + (StatusCode::UNAUTHORIZED, message.into()) + } +} + +impl ClientError for BearerError { + fn client_response(&self) -> (StatusCode, Cow<'_, str>) { + match self { + Self::InvalidCredentials => (StatusCode::UNAUTHORIZED, "Invalid credentials".into()), + Self::Repository(error) => error.client_response(), + } + } +} + +impl ClientError for DomainError { + fn client_response(&self) -> (StatusCode, Cow<'_, str>) { + match self { + Self::Amount(error) => (StatusCode::UNPROCESSABLE_ENTITY, error.to_string().into()), + Self::Schedule(error) => (StatusCode::UNPROCESSABLE_ENTITY, error.to_string().into()), + } + } +} + +impl ClientError for RepositoryError { + fn client_response(&self) -> (StatusCode, Cow<'_, str>) { + match self { + Self::Database(_) | Self::Decode(_) => internal_error(), + Self::QueryConversion(error) => (StatusCode::BAD_REQUEST, error.to_string().into()), + Self::ReferenceViolation { relation, kind, .. } => { + use crate::repository::{ + ReferenceRelation as Relation, ReferenceViolationKind as Kind, + }; + match (relation, kind) { + (Relation::Account, Kind::Missing) => ( + StatusCode::UNPROCESSABLE_ENTITY, + "Account cannot be found".into(), + ), + (Relation::RecurringTransaction, Kind::StillReferenced) => ( + StatusCode::CONFLICT, + "Still referenced by one or more recurring transactions".into(), + ), + (Relation::Transaction, Kind::StillReferenced) => ( + StatusCode::CONFLICT, + "Still referenced by one or more transactions".into(), + ), + (Relation::User, Kind::Missing) => { + (StatusCode::NOT_FOUND, "User cannot be found".into()) + } + (Relation::UserApp, Kind::Missing) => { + (StatusCode::NOT_FOUND, "User app cannot be found".into()) + } + _ => internal_error(), + } + } + } } } @@ -164,7 +172,3 @@ fn error_response(status_code: StatusCode, message: impl Into) -> Respon ) .into_response() } - -pub trait ClientError { - fn client_error_message(&self) -> &'static str; -} diff --git a/server/src/routes/extractors/auth/bearer.rs b/server/src/routes/extractors/auth/bearer.rs index 144e1da..4da6e85 100644 --- a/server/src/routes/extractors/auth/bearer.rs +++ b/server/src/routes/extractors/auth/bearer.rs @@ -52,12 +52,3 @@ pub enum BearerError { #[error(transparent)] Repository(#[from] RepositoryError), } - -impl crate::error::ClientError for BearerError { - fn client_error_message(&self) -> &'static str { - match self { - BearerError::InvalidCredentials => "Invalid credentials", - BearerError::Repository(_) => "Repository error", - } - } -} diff --git a/server/src/routes/extractors/auth/header.rs b/server/src/routes/extractors/auth/header.rs index f625b1a..9213fbf 100644 --- a/server/src/routes/extractors/auth/header.rs +++ b/server/src/routes/extractors/auth/header.rs @@ -75,18 +75,6 @@ pub enum AuthHeaderError { EmptyToken, } -impl crate::error::ClientError for AuthHeaderError { - fn client_error_message(&self) -> &'static str { - match self { - AuthHeaderError::Missing => "Missing authorization header", - AuthHeaderError::NotAscii(_) => "Invalid authorization header encoding", - AuthHeaderError::MalformedValue => "Malformed authorization header", - AuthHeaderError::EmptyScheme => "Empty authorization scheme", - AuthHeaderError::EmptyToken => "Empty authorization token", - } - } -} - impl IntoResponse for AuthHeaderError { fn into_response(self) -> axum::response::Response { Into::::into(self).into_response()