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 {
|
|
|
|
Ok(payload) => Ok(payload),
|
|
|
|
Err(JsonRejection::MissingJsonContentType(_)) => Err(ServerError::MissingJsonContentType),
|
|
|
|
Err(JsonRejection::JsonDataError(_)) => Err(ServerError::JsonDataError),
|
|
|
|
Err(JsonRejection::JsonSyntaxError(_)) => Err(ServerError::JsonSyntaxError),
|
|
|
|
Err(JsonRejection::BytesRejection(_)) => Err(ServerError::BytesRejection),
|
|
|
|
Err(err) => Err(ServerError::Other(err.to_string())),
|
|
|
|
}
|
|
|
|
}
|