forked from OKEAMAH/didkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.rs
More file actions
36 lines (32 loc) · 1.03 KB
/
utils.rs
File metadata and controls
36 lines (32 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use axum::{
async_trait,
extract::{rejection::JsonRejection, FromRequest},
http::Request,
http::StatusCode,
};
pub struct CustomErrorJson<T>(pub T);
#[async_trait]
impl<S, B, T> FromRequest<S, B> for CustomErrorJson<T>
where
axum::Json<T>: FromRequest<S, B, Rejection = JsonRejection>,
S: Send + Sync,
B: Send + 'static,
{
type Rejection = (StatusCode, String);
async fn from_request(req: Request<B>, state: &S) -> Result<Self, Self::Rejection> {
let (parts, body) = req.into_parts();
let req = Request::from_parts(parts, body);
match axum::Json::<T>::from_request(req, state).await {
Ok(value) => Ok(Self(value.0)),
Err(rejection) => {
let message = rejection.to_string();
let code = if let JsonRejection::JsonDataError(_) = rejection {
StatusCode::BAD_REQUEST
} else {
rejection.status()
};
Err((code, message))
}
}
}
}