Continuing with error refactoring.

This commit is contained in:
2026-10-10 20:50:43 +02:00
parent d6bb67721e
commit 731139045d
18 changed files with 359 additions and 103 deletions
+13 -3
View File
@@ -3,7 +3,7 @@ use axum::response::IntoResponse;
use serde::Serialize;
use serde::de::DeserializeOwned;
use crate::error::AppError;
use crate::routes::extractors::ExtractorError;
pub struct Json<T>(pub T);
@@ -12,13 +12,13 @@ where
T: DeserializeOwned,
S: Send + Sync,
{
type Rejection = AppError;
type Rejection = JsonError;
async fn from_request(request: Request, state: &S) -> Result<Self, Self::Rejection> {
axum::Json::<T>::from_request(request, state)
.await
.map(|axum::Json(value)| Self(value))
.map_err(|rejection| AppError::BadRequest(rejection.body_text()))
.map_err(Into::into)
}
}
@@ -30,3 +30,13 @@ where
axum::Json(self.0).into_response()
}
}
#[derive(Debug, thiserror::Error)]
#[error("json error")]
pub struct JsonError(#[from] axum::extract::rejection::JsonRejection);
impl IntoResponse for JsonError {
fn into_response(self) -> axum::response::Response {
ExtractorError::Json(self).into_response()
}
}