Continuing with error refactoring.
This commit is contained in:
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user