From b49bb08c7a77fe4b29cf4ed0504c2f1c32a025e3 Mon Sep 17 00:00:00 2001 From: batsumaru <> Date: Fri, 16 Oct 2026 15:17:17 +0900 Subject: Update error responses --- src/handlers.rs | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) (limited to 'src/handlers.rs') diff --git a/src/handlers.rs b/src/handlers.rs index b048681..50ae9e2 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -1,5 +1,5 @@ use axum::{ - extract::{Path, Query, State}, + extract::State, http::{HeaderName, StatusCode, header}, response::Json, }; @@ -9,6 +9,7 @@ use strum::IntoEnumIterator; use uuid::Uuid; use crate::error::AppError; +use crate::extract::{AppJson, AppPath, AppQuery}; use crate::models::{ ListQuery, MeasurementData, MeasurementView, MetricType, MetricUnitPair, Order, Unit, }; @@ -19,28 +20,32 @@ pub async fn health() -> StatusCode { pub async fn list_measurements( State(pool): State, - Query(query): Query, + AppQuery(query): AppQuery, ) -> Result>, AppError> { let limit = match query.limit { Some(x) if (x > 0) => Some(x), None => None, - _ => return Err(AppError::BadRequest), + _ => return Err(AppError::BadRequest("limit must be positive")), }; let (from, to) = match (query.date, query.from, query.to) { (Some(date), None, None) => (Some(date), Some(date)), (None, from, to) => (from, to), - _ => return Err(AppError::BadRequest), + _ => { + return Err(AppError::BadRequest( + "date can't be combined with from or to", + )); + } }; if let (Some(from), Some(to)) = (from, to) && from > to { - return Err(AppError::BadRequest); + return Err(AppError::BadRequest("from must not be after to")); } let start = from.map(|from| from.and_time(NaiveTime::MIN).and_utc()); let end = match to { Some(to) => { let Some(next_day) = to.succ_opt() else { - return Err(AppError::BadRequest); + return Err(AppError::BadRequest("to is out of range")); }; Some(next_day.and_time(NaiveTime::MIN).and_utc()) } @@ -72,7 +77,7 @@ pub async fn list_measurements( pub async fn create_measurement( State(pool): State, - Json(data): Json, + AppJson(data): AppJson, ) -> Result<(StatusCode, [(HeaderName, String); 1], Json), AppError> { let measurement = sqlx::query_as!( MeasurementView, @@ -99,7 +104,7 @@ pub async fn create_measurement( pub async fn get_measurement( State(pool): State, - Path(id): Path, + AppPath(id): AppPath, ) -> Result, AppError> { let measurement = sqlx::query_as!( MeasurementView, @@ -116,8 +121,8 @@ pub async fn get_measurement( pub async fn update_measurement( State(pool): State, - Path(id): Path, - Json(data): Json, + AppPath(id): AppPath, + AppJson(data): AppJson, ) -> Result { let result = sqlx::query!( " @@ -144,7 +149,7 @@ pub async fn update_measurement( pub async fn delete_measurement( State(pool): State, - Path(id): Path, + AppPath(id): AppPath, ) -> Result { sqlx::query!("DELETE FROM measurement WHERE id = $1", id) .execute(&pool) -- cgit v1.3