#[cfg(feature = "uploads")]
use axum::extract::multipart::MultipartRejection;
use axum::extract::rejection::{FormRejection, JsonRejection, PathRejection, QueryRejection};
use crate::api::{Problem, ProblemKind};
#[must_use]
pub fn from_json_rejection(rejection: &JsonRejection) -> Problem {
match rejection {
JsonRejection::MissingJsonContentType(_) => Problem::of(ProblemKind::UnsupportedMediaType)
.with_detail("Request body must be application/json"),
JsonRejection::JsonSyntaxError(_) => {
Problem::of(ProblemKind::MalformedJson).with_detail("Request body is not valid JSON")
}
JsonRejection::JsonDataError(_) => Problem::of(ProblemKind::Validation)
.with_detail("Request body JSON does not match the expected schema"),
JsonRejection::BytesRejection(_) => {
Problem::of(ProblemKind::PayloadTooLarge).with_detail("Request body could not be read")
}
_ => Problem::of(ProblemKind::BadRequest).with_detail("Request body could not be parsed"),
}
}
#[must_use]
pub fn from_query_rejection(rejection: &QueryRejection) -> Problem {
match rejection {
QueryRejection::FailedToDeserializeQueryString(_) => {
Problem::of(ProblemKind::BadRequest).with_detail("Query string is malformed")
}
_ => Problem::of(ProblemKind::BadRequest).with_detail("Query string is malformed"),
}
}
#[must_use]
pub fn from_path_rejection(rejection: &PathRejection) -> Problem {
match rejection {
PathRejection::FailedToDeserializePathParams(_) => {
Problem::of(ProblemKind::BadRequest).with_detail("Path parameters are malformed")
}
PathRejection::MissingPathParams(_) => Problem::of(ProblemKind::Internal)
.with_detail("Route could not be matched to path parameters"),
_ => Problem::of(ProblemKind::BadRequest).with_detail("Path parameters are malformed"),
}
}
#[must_use]
pub fn from_form_rejection(rejection: &FormRejection) -> Problem {
match rejection {
FormRejection::InvalidFormContentType(_) => Problem::of(ProblemKind::UnsupportedMediaType)
.with_detail("Form request must be application/x-www-form-urlencoded"),
FormRejection::FailedToDeserializeForm(_) => {
Problem::of(ProblemKind::BadRequest).with_detail("Form body is malformed")
}
FormRejection::FailedToDeserializeFormBody(_) => Problem::of(ProblemKind::Validation)
.with_detail("Form body does not match the expected schema"),
FormRejection::BytesRejection(_) => {
Problem::of(ProblemKind::PayloadTooLarge).with_detail("Form body could not be read")
}
_ => Problem::of(ProblemKind::BadRequest).with_detail("Form body is malformed"),
}
}
#[cfg(feature = "uploads")]
#[must_use]
pub fn from_multipart_rejection(rejection: &MultipartRejection) -> Problem {
match rejection {
MultipartRejection::InvalidBoundary(_) => Problem::of(ProblemKind::UnsupportedMediaType)
.with_detail("Request body must be multipart/form-data with a boundary"),
_ => Problem::of(ProblemKind::BadRequest)
.with_detail("Request body could not be read as multipart/form-data"),
}
}