Continuing with error refactoring.
This commit is contained in:
@@ -8,7 +8,7 @@ pub use bearer::*;
|
||||
pub use header::*;
|
||||
pub use principal::*;
|
||||
|
||||
use crate::{error::AppError, repository::RepositoryError};
|
||||
use crate::{repository::RepositoryError, routes::extractors::ExtractorError};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Auth<T = Bearer>(pub Principal<T>);
|
||||
@@ -55,7 +55,7 @@ pub enum AuthError {
|
||||
|
||||
impl IntoResponse for AuthError {
|
||||
fn into_response(self) -> axum::response::Response {
|
||||
AppError::from(self).into_response()
|
||||
ExtractorError::from(self).into_response()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
use axum::extract::{FromRequest, Request};
|
||||
use axum::{
|
||||
extract::{FromRequest, Request},
|
||||
response::IntoResponse,
|
||||
};
|
||||
use serde::de::DeserializeOwned;
|
||||
|
||||
use crate::error::AppError;
|
||||
use crate::routes::extractors::ExtractorError;
|
||||
|
||||
pub struct Form<T>(pub T);
|
||||
|
||||
@@ -10,12 +13,22 @@ where
|
||||
T: DeserializeOwned,
|
||||
S: Send + Sync,
|
||||
{
|
||||
type Rejection = AppError;
|
||||
type Rejection = FormError;
|
||||
|
||||
async fn from_request(request: Request, state: &S) -> Result<Self, Self::Rejection> {
|
||||
axum::Form::<T>::from_request(request, state)
|
||||
.await
|
||||
.map(|axum::Form(value)| Self(value))
|
||||
.map_err(|rejection| AppError::BadRequest(rejection.body_text()))
|
||||
.map_err(Into::into)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
#[error("form error")]
|
||||
pub struct FormError(#[from] axum::extract::rejection::FormRejection);
|
||||
|
||||
impl IntoResponse for FormError {
|
||||
fn into_response(self) -> axum::response::Response {
|
||||
ExtractorError::Form(self).into_response()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
use axum::extract::FromRequestParts;
|
||||
use axum::response::IntoResponse;
|
||||
use serde::de::DeserializeOwned;
|
||||
|
||||
use crate::error::AppError;
|
||||
use crate::routes::extractors::ExtractorError;
|
||||
|
||||
pub struct Path<T>(pub T);
|
||||
|
||||
@@ -10,7 +11,7 @@ where
|
||||
T: DeserializeOwned + Send,
|
||||
S: Send + Sync,
|
||||
{
|
||||
type Rejection = AppError;
|
||||
type Rejection = PathError;
|
||||
|
||||
async fn from_request_parts(
|
||||
parts: &mut axum::http::request::Parts,
|
||||
@@ -19,6 +20,16 @@ where
|
||||
axum::extract::Path::<T>::from_request_parts(parts, state)
|
||||
.await
|
||||
.map(|axum::extract::Path(value)| Self(value))
|
||||
.map_err(|rejection| AppError::BadRequest(rejection.body_text()))
|
||||
.map_err(Into::into)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
#[error("path error")]
|
||||
pub struct PathError(#[from] axum::extract::rejection::PathRejection);
|
||||
|
||||
impl IntoResponse for PathError {
|
||||
fn into_response(self) -> axum::response::Response {
|
||||
ExtractorError::Path(self).into_response()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
use axum::extract::FromRequestParts;
|
||||
use axum::response::IntoResponse;
|
||||
use serde::de::DeserializeOwned;
|
||||
|
||||
use crate::error::AppError;
|
||||
use crate::routes::extractors::ExtractorError;
|
||||
|
||||
pub struct Query<T>(pub T);
|
||||
|
||||
@@ -10,7 +11,7 @@ where
|
||||
T: DeserializeOwned + Send,
|
||||
S: Send + Sync,
|
||||
{
|
||||
type Rejection = AppError;
|
||||
type Rejection = QueryError;
|
||||
|
||||
async fn from_request_parts(
|
||||
parts: &mut axum::http::request::Parts,
|
||||
@@ -19,6 +20,16 @@ where
|
||||
axum::extract::Query::<T>::from_request_parts(parts, state)
|
||||
.await
|
||||
.map(|axum::extract::Query(value)| Self(value))
|
||||
.map_err(|rejection| AppError::BadRequest(rejection.body_text()))
|
||||
.map_err(Into::into)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
#[error("query error")]
|
||||
pub struct QueryError(#[from] axum::extract::rejection::QueryRejection);
|
||||
|
||||
impl IntoResponse for QueryError {
|
||||
fn into_response(self) -> axum::response::Response {
|
||||
ExtractorError::Query(self).into_response()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user