use rama::http::service::web::response::IntoResponse;
use rama::http::{header, Response, StatusCode};
#[derive(Debug)]
pub struct VldRejection {
error: vld::error::VldError,
status: StatusCode,
}
impl VldRejection {
#[must_use]
pub fn from_vld(error: vld::error::VldError) -> Self {
Self {
error,
status: StatusCode::UNPROCESSABLE_ENTITY,
}
}
#[must_use]
pub fn parse(message: impl Into<String>) -> Self {
Self::from_vld(vld::error::VldError::single(
vld::error::IssueCode::ParseError,
message.into(),
))
}
#[must_use]
pub fn payload_too_large() -> Self {
Self {
error: vld::error::VldError::single(
vld::error::IssueCode::ParseError,
"Payload too large",
),
status: StatusCode::PAYLOAD_TOO_LARGE,
}
}
#[must_use]
pub fn error(&self) -> &vld::error::VldError {
&self.error
}
}
impl IntoResponse for VldRejection {
fn into_response(self) -> Response {
let body = if self.status == StatusCode::PAYLOAD_TOO_LARGE {
vld_http_common::format_payload_too_large()
} else {
vld_http_common::format_vld_error(&self.error)
};
(
self.status,
[(header::CONTENT_TYPE, "application/json")],
body.to_string(),
)
.into_response()
}
}
impl std::fmt::Display for VldRejection {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Validation failed: {}", self.error)
}
}
impl std::error::Error for VldRejection {}