From 46a127fbf1fc3bfd34c971056635ce0ef56cbd40 Mon Sep 17 00:00:00 2001 From: Rogier Reerink Date: Sun, 11 Oct 2026 08:45:00 +0200 Subject: [PATCH] Full extractor responses. --- server/src/routes/errors.rs | 36 ++++++++++----------- server/src/routes/extractors/auth/header.rs | 4 +-- server/src/routes/extractors/form.rs | 2 +- server/src/routes/extractors/json.rs | 2 +- server/src/routes/extractors/path.rs | 2 +- server/src/routes/extractors/query.rs | 2 +- 6 files changed, 24 insertions(+), 24 deletions(-) diff --git a/server/src/routes/errors.rs b/server/src/routes/errors.rs index 1713e0a..2e39427 100644 --- a/server/src/routes/errors.rs +++ b/server/src/routes/errors.rs @@ -8,6 +8,20 @@ use axum::{ use super::extractors::{AuthError, AuthHeaderError, BearerError, ExtractorError}; use crate::{domain::DomainError, error::AppError, repository::RepositoryError}; +impl IntoResponse for AppError { + fn into_response(self) -> Response { + let (status, message) = self.client_response(); + + if status.is_server_error() { + tracing::error!(error = ?self, %status, "request failed"); + } else { + tracing::debug!(error = ?self, %status, "request rejected"); + } + + error_response(status, message) + } +} + impl ClientError for AppError { fn client_response(&self) -> (StatusCode, Cow<'_, str>) { match self { @@ -42,10 +56,10 @@ impl ClientError for ExtractorError { fn client_response(&self) -> (StatusCode, Cow<'_, str>) { match self { Self::Authorization(error) => error.client_response(), - Self::Form(_) => (StatusCode::BAD_REQUEST, "Form error".into()), - Self::Json(_) => (StatusCode::BAD_REQUEST, "Json error".into()), - Self::Path(_) => (StatusCode::BAD_REQUEST, "Path error".into()), - Self::Query(_) => (StatusCode::BAD_REQUEST, "Query error".into()), + Self::Form(error) => (StatusCode::BAD_REQUEST, error.to_string().into()), + Self::Json(error) => (StatusCode::BAD_REQUEST, error.to_string().into()), + Self::Path(error) => (StatusCode::BAD_REQUEST, error.to_string().into()), + Self::Query(error) => (StatusCode::BAD_REQUEST, error.to_string().into()), } } } @@ -130,20 +144,6 @@ fn internal_error() -> (StatusCode, Cow<'static, str>) { ) } -impl IntoResponse for AppError { - fn into_response(self) -> Response { - let (status, message) = self.client_response(); - - if status.is_server_error() { - tracing::error!(error = ?self, %status, "request failed"); - } else { - tracing::debug!(error = ?self, %status, "request rejected"); - } - - error_response(status, message) - } -} - #[derive(serde::Serialize)] struct ErrorResponse { pub message: String, diff --git a/server/src/routes/extractors/auth/header.rs b/server/src/routes/extractors/auth/header.rs index 9213fbf..688fd09 100644 --- a/server/src/routes/extractors/auth/header.rs +++ b/server/src/routes/extractors/auth/header.rs @@ -62,8 +62,8 @@ pub enum AuthHeaderError { #[error("missing header")] Missing, - #[error("invalid encoding")] - NotAscii(#[source] ToStrError), + #[error(transparent)] + NotAscii(#[from] ToStrError), #[error("malformed value")] MalformedValue, diff --git a/server/src/routes/extractors/form.rs b/server/src/routes/extractors/form.rs index 5db58c4..1c71e47 100644 --- a/server/src/routes/extractors/form.rs +++ b/server/src/routes/extractors/form.rs @@ -24,7 +24,7 @@ where } #[derive(Debug, thiserror::Error)] -#[error("form error")] +#[error(transparent)] pub struct FormError(#[from] axum::extract::rejection::FormRejection); impl IntoResponse for FormError { diff --git a/server/src/routes/extractors/json.rs b/server/src/routes/extractors/json.rs index c0b33ca..406a8df 100644 --- a/server/src/routes/extractors/json.rs +++ b/server/src/routes/extractors/json.rs @@ -32,7 +32,7 @@ where } #[derive(Debug, thiserror::Error)] -#[error("json error")] +#[error(transparent)] pub struct JsonError(#[from] axum::extract::rejection::JsonRejection); impl IntoResponse for JsonError { diff --git a/server/src/routes/extractors/path.rs b/server/src/routes/extractors/path.rs index 41d1a40..c6863c7 100644 --- a/server/src/routes/extractors/path.rs +++ b/server/src/routes/extractors/path.rs @@ -25,7 +25,7 @@ where } #[derive(Debug, thiserror::Error)] -#[error("path error")] +#[error(transparent)] pub struct PathError(#[from] axum::extract::rejection::PathRejection); impl IntoResponse for PathError { diff --git a/server/src/routes/extractors/query.rs b/server/src/routes/extractors/query.rs index cc4582c..1281e9e 100644 --- a/server/src/routes/extractors/query.rs +++ b/server/src/routes/extractors/query.rs @@ -25,7 +25,7 @@ where } #[derive(Debug, thiserror::Error)] -#[error("query error")] +#[error(transparent)] pub struct QueryError(#[from] axum::extract::rejection::QueryRejection); impl IntoResponse for QueryError {