From 37a2b7b5dc70685257003b3484efcb728b0d3949 Mon Sep 17 00:00:00 2001 From: Rogier Reerink Date: Fri, 25 Sep 2026 17:32:54 +0200 Subject: [PATCH] Return JSON error responses. --- server/src/error.rs | 31 +++++++++++++++++--- server/src/routes.rs | 5 ++-- server/src/routes/extractors.rs | 2 ++ server/src/routes/extractors/json.rs | 32 +++++++++++++++++++++ server/src/routes/extractors/path.rs | 24 ++++++++++++++++ server/src/routes/scheduled_transactions.rs | 7 ++--- server/src/routes/transactions.rs | 7 ++--- 7 files changed, 92 insertions(+), 16 deletions(-) create mode 100644 server/src/routes/extractors.rs create mode 100644 server/src/routes/extractors/json.rs create mode 100644 server/src/routes/extractors/path.rs diff --git a/server/src/error.rs b/server/src/error.rs index 7f0a37d..a3e1091 100644 --- a/server/src/error.rs +++ b/server/src/error.rs @@ -17,6 +17,9 @@ use crate::routes::{ #[derive(Debug, thiserror::Error)] pub enum AppError { + #[error("bad request")] + BadRequest(String), + #[error("not found")] NotFound, @@ -42,14 +45,17 @@ pub enum AppError { impl IntoResponse for AppError { fn into_response(self) -> Response { match self { + Self::BadRequest(error) => { + return error_response(StatusCode::BAD_REQUEST, error); + } Self::NotFound => { - return StatusCode::NOT_FOUND.into_response(); + return error_response(StatusCode::NOT_FOUND, "Not found"); } Self::ScheduledTransactionValidation(error) => { - return (StatusCode::UNPROCESSABLE_ENTITY, error.to_string()).into_response(); + return error_response(StatusCode::UNPROCESSABLE_ENTITY, error.to_string()); } Self::TransactionValidation(error) => { - return (StatusCode::UNPROCESSABLE_ENTITY, error.to_string()).into_response(); + return error_response(StatusCode::UNPROCESSABLE_ENTITY, error.to_string()); } Self::Database(error) => { tracing::error!(%error, "Database query failed"); @@ -65,6 +71,23 @@ impl IntoResponse for AppError { } } - (StatusCode::INTERNAL_SERVER_ERROR, "Internal server error").into_response() + error_response(StatusCode::INTERNAL_SERVER_ERROR, "Internal server error") } } + +#[derive(serde::Serialize)] +pub struct ErrorResponse { + pub message: String, +} + +impl ErrorResponse { + pub fn new(message: impl Into) -> Self { + Self { + message: message.into(), + } + } +} + +fn error_response(status_code: StatusCode, message: impl Into) -> Response { + (status_code, axum::Json(ErrorResponse::new(message))).into_response() +} diff --git a/server/src/routes.rs b/server/src/routes.rs index 327d0ba..69c3139 100644 --- a/server/src/routes.rs +++ b/server/src/routes.rs @@ -1,6 +1,7 @@ pub mod dtos; -mod scheduled_transactions; -mod transactions; +pub mod extractors; +pub mod scheduled_transactions; +pub mod transactions; pub mod validation; pub fn create_router(pool: sqlx::PgPool) -> axum::Router { diff --git a/server/src/routes/extractors.rs b/server/src/routes/extractors.rs new file mode 100644 index 0000000..bccb7a4 --- /dev/null +++ b/server/src/routes/extractors.rs @@ -0,0 +1,2 @@ +pub mod json; +pub mod path; diff --git a/server/src/routes/extractors/json.rs b/server/src/routes/extractors/json.rs new file mode 100644 index 0000000..085de24 --- /dev/null +++ b/server/src/routes/extractors/json.rs @@ -0,0 +1,32 @@ +use axum::extract::{FromRequest, Request}; +use axum::response::IntoResponse; +use serde::Serialize; +use serde::de::DeserializeOwned; + +use crate::error::AppError; + +pub struct Json(pub T); + +impl FromRequest for Json +where + T: DeserializeOwned, + S: Send + Sync, +{ + type Rejection = AppError; + + async fn from_request(request: Request, state: &S) -> Result { + axum::Json::::from_request(request, state) + .await + .map(|axum::Json(value)| Self(value)) + .map_err(|rejection| AppError::BadRequest(rejection.body_text())) + } +} + +impl IntoResponse for Json +where + T: Serialize, +{ + fn into_response(self) -> axum::response::Response { + axum::Json(self.0).into_response() + } +} diff --git a/server/src/routes/extractors/path.rs b/server/src/routes/extractors/path.rs new file mode 100644 index 0000000..19ed812 --- /dev/null +++ b/server/src/routes/extractors/path.rs @@ -0,0 +1,24 @@ +use axum::extract::FromRequestParts; +use serde::de::DeserializeOwned; + +use crate::error::AppError; + +pub struct Path(pub T); + +impl FromRequestParts for Path +where + T: DeserializeOwned + Send, + S: Send + Sync, +{ + type Rejection = AppError; + + async fn from_request_parts( + parts: &mut axum::http::request::Parts, + state: &S, + ) -> Result { + axum::extract::Path::::from_request_parts(parts, state) + .await + .map(|axum::extract::Path(value)| Self(value)) + .map_err(|rejection| AppError::BadRequest(rejection.body_text())) + } +} diff --git a/server/src/routes/scheduled_transactions.rs b/server/src/routes/scheduled_transactions.rs index 9247d7c..bf335f5 100644 --- a/server/src/routes/scheduled_transactions.rs +++ b/server/src/routes/scheduled_transactions.rs @@ -1,8 +1,4 @@ -use axum::{ - Json, - extract::{Path, State}, - http::StatusCode, -}; +use axum::{extract::State, http::StatusCode}; use sqlx::PgPool; use uuid::Uuid; @@ -20,6 +16,7 @@ use crate::{ dtos::scheduled_transaction::{ RecurringTransaction, ScheduledTransaction, ScheduledTransactionData, }, + extractors::{json::Json, path::Path}, validation::scheduled_transaction::ValidatedRecurringTransaction, }, }; diff --git a/server/src/routes/transactions.rs b/server/src/routes/transactions.rs index 9cdee9d..e9a700b 100644 --- a/server/src/routes/transactions.rs +++ b/server/src/routes/transactions.rs @@ -1,8 +1,4 @@ -use axum::{ - Json, - extract::{Path, State}, - http::StatusCode, -}; +use axum::{extract::State, http::StatusCode}; use sqlx::PgPool; use uuid::Uuid; @@ -17,6 +13,7 @@ use crate::{ error::AppError, routes::{ dtos::transaction::{Transaction, TransactionData}, + extractors::{json::Json, path::Path}, validation::transaction::ValidatedTransaction, }, };