2022-04-23 19:21:04 +00:00
|
|
|
use axum::{extract::rejection::JsonRejection, Json};
|
|
|
|
use homedisk_types::errors::ServerError;
|
|
|
|
|
2022-06-08 17:08:06 +00:00
|
|
|
/// Validate json request
|
2022-04-23 19:21:04 +00:00
|
|
|
pub fn validate_json<Typ>(
|
|
|
|
payload: Result<Json<Typ>, JsonRejection>,
|
|
|
|
) -> Result<Json<Typ>, ServerError> {
|
|
|
|
match payload {
|
2022-06-11 08:19:47 +00:00
|
|
|
// if success return payload
|
2022-04-23 19:21:04 +00:00
|
|
|
Ok(payload) => Ok(payload),
|
2022-06-11 08:19:47 +00:00
|
|
|
// mission json in Content-Type Header
|
2022-04-23 19:21:04 +00:00
|
|
|
Err(JsonRejection::MissingJsonContentType(_)) => Err(ServerError::MissingJsonContentType),
|
2022-06-11 08:19:47 +00:00
|
|
|
// failed to deserialize json
|
2022-04-23 19:21:04 +00:00
|
|
|
Err(JsonRejection::JsonDataError(_)) => Err(ServerError::JsonDataError),
|
2022-06-11 08:19:47 +00:00
|
|
|
// syntax error in json
|
2022-04-23 19:21:04 +00:00
|
|
|
Err(JsonRejection::JsonSyntaxError(_)) => Err(ServerError::JsonSyntaxError),
|
2022-06-11 08:19:47 +00:00
|
|
|
// failed to extract the request body
|
2022-04-23 19:21:04 +00:00
|
|
|
Err(JsonRejection::BytesRejection(_)) => Err(ServerError::BytesRejection),
|
2022-06-11 08:19:47 +00:00
|
|
|
// other error
|
2022-04-23 19:21:04 +00:00
|
|
|
Err(err) => Err(ServerError::Other(err.to_string())),
|
|
|
|
}
|
|
|
|
}
|