|
| 1 | +use anyhow::anyhow; |
| 2 | +use axum::{ |
| 3 | + extract::{ |
| 4 | + Request, |
| 5 | + rejection::{ExtensionRejection, JsonRejection}, |
| 6 | + {FromRequest, FromRequestParts}, |
| 7 | + }, |
| 8 | + response::IntoResponse, |
| 9 | +}; |
| 10 | +use axum_extra::extract::QueryRejection; |
| 11 | +use http::request::Parts; |
| 12 | +use serde::Serialize; |
| 13 | + |
| 14 | +use crate::{error_response::ApiError, errors::ApiBadRequest}; |
| 15 | + |
| 16 | +pub struct ExtractorError(ApiError); |
| 17 | + |
| 18 | +impl IntoResponse for ExtractorError { |
| 19 | + fn into_response(self) -> axum::response::Response { |
| 20 | + let mut res = self.0.into_response(); |
| 21 | + |
| 22 | + res.extensions_mut().insert(FailedExtraction); |
| 23 | + |
| 24 | + res |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +#[derive(Clone, Copy)] |
| 29 | +pub struct FailedExtraction; |
| 30 | + |
| 31 | +pub struct Json<T>(pub T); |
| 32 | + |
| 33 | +impl<S, T> FromRequest<S> for Json<T> |
| 34 | +where |
| 35 | + axum::extract::Json<T>: FromRequest<S, Rejection = JsonRejection>, |
| 36 | + S: Send + Sync, |
| 37 | +{ |
| 38 | + type Rejection = ExtractorError; |
| 39 | + |
| 40 | + async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> { |
| 41 | + axum::extract::Json::<T>::from_request(req, state) |
| 42 | + .await |
| 43 | + .map(|json| Json(json.0)) |
| 44 | + .map_err(|err| { |
| 45 | + ExtractorError( |
| 46 | + ApiBadRequest { |
| 47 | + reason: err.body_text(), |
| 48 | + } |
| 49 | + .build() |
| 50 | + .into(), |
| 51 | + ) |
| 52 | + }) |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +impl<T: Serialize> IntoResponse for Json<T> { |
| 57 | + fn into_response(self) -> axum::response::Response { |
| 58 | + let Self(value) = self; |
| 59 | + axum::extract::Json(value).into_response() |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +pub struct Query<T>(pub T); |
| 64 | + |
| 65 | +impl<S, T> FromRequestParts<S> for Query<T> |
| 66 | +where |
| 67 | + axum_extra::extract::Query<T>: FromRequestParts<S, Rejection = QueryRejection>, |
| 68 | + S: Send + Sync, |
| 69 | +{ |
| 70 | + type Rejection = ExtractorError; |
| 71 | + |
| 72 | + async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> { |
| 73 | + let res = axum_extra::extract::Query::<T>::from_request_parts(parts, state) |
| 74 | + .await |
| 75 | + .map(|query| Query(query.0)) |
| 76 | + .map_err(|err| { |
| 77 | + ExtractorError( |
| 78 | + ApiBadRequest { |
| 79 | + reason: err.body_text(), |
| 80 | + } |
| 81 | + .build() |
| 82 | + .into(), |
| 83 | + ) |
| 84 | + }); |
| 85 | + |
| 86 | + res |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +pub struct Extension<T>(pub T); |
| 91 | + |
| 92 | +impl<S, T> FromRequestParts<S> for Extension<T> |
| 93 | +where |
| 94 | + axum::extract::Extension<T>: FromRequestParts<S, Rejection = ExtensionRejection>, |
| 95 | + S: Send + Sync, |
| 96 | +{ |
| 97 | + type Rejection = ExtractorError; |
| 98 | + |
| 99 | + async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> { |
| 100 | + axum::extract::Extension::<T>::from_request_parts(parts, state) |
| 101 | + .await |
| 102 | + .map(|ext| Extension(ext.0)) |
| 103 | + .map_err(|err| { |
| 104 | + ExtractorError( |
| 105 | + anyhow!("developer error: extension error: {}", err.body_text()).into(), |
| 106 | + ) |
| 107 | + }) |
| 108 | + } |
| 109 | +} |
0 commit comments