use crate::response::Response;
use ubyte::ByteUnit;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum ExtractJsonBodyError {
#[error(transparent)]
MissingContentType(#[from] MissingJsonContentType),
#[error(transparent)]
ContentTypeMismatch(#[from] JsonContentTypeMismatch),
#[error(transparent)]
DeserializationError(#[from] JsonDeserializationError),
}
impl ExtractJsonBodyError {
pub fn into_response(&self) -> Response {
match self {
ExtractJsonBodyError::MissingContentType(_)
| ExtractJsonBodyError::ContentTypeMismatch(_) => Response::unsupported_media_type(),
ExtractJsonBodyError::DeserializationError(_) => Response::bad_request(),
}
.set_typed_body(format!("{}", self))
}
}
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum ExtractBufferedBodyError {
#[error(transparent)]
SizeLimitExceeded(#[from] SizeLimitExceeded),
#[error(transparent)]
UnexpectedBufferError(#[from] UnexpectedBufferError),
}
impl ExtractBufferedBodyError {
pub fn into_response(&self) -> Response {
match self {
ExtractBufferedBodyError::SizeLimitExceeded(_) => Response::payload_too_large(),
ExtractBufferedBodyError::UnexpectedBufferError(_) => Response::internal_server_error(),
}
.set_typed_body(format!("{}", self))
}
}
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum ExtractUrlEncodedBodyError {
#[error(transparent)]
MissingContentType(#[from] MissingUrlEncodedContentType),
#[error(transparent)]
ContentTypeMismatch(#[from] UrlEncodedContentTypeMismatch),
#[error(transparent)]
DeserializationError(#[from] UrlEncodedBodyDeserializationError),
}
impl ExtractUrlEncodedBodyError {
pub fn into_response(&self) -> Response {
match self {
ExtractUrlEncodedBodyError::MissingContentType(_)
| ExtractUrlEncodedBodyError::ContentTypeMismatch(_) => {
Response::unsupported_media_type()
}
ExtractUrlEncodedBodyError::DeserializationError(_) => Response::bad_request(),
}
.set_typed_body(format!("{}", self))
}
}
#[derive(Debug, thiserror::Error)]
#[error("The request body is larger than the maximum size limit enforced by this server.")]
#[non_exhaustive]
pub struct SizeLimitExceeded {
pub max_size: ByteUnit,
pub content_length: Option<usize>,
}
#[derive(Debug, thiserror::Error)]
#[error("Something went wrong while reading the request body.")]
#[non_exhaustive]
pub struct UnexpectedBufferError {
#[source]
pub(super) source: Box<dyn std::error::Error + Send + Sync>,
}
#[derive(Debug, thiserror::Error)]
#[error(
"The `Content-Type` header is missing. This endpoint expects requests with a `Content-Type` header set to `application/json`, or another `application/*+json` MIME type"
)]
#[non_exhaustive]
pub struct MissingJsonContentType;
#[derive(Debug, thiserror::Error)]
#[error("Failed to deserialize the body as a JSON document.\n{source}")]
#[non_exhaustive]
pub struct JsonDeserializationError {
#[source]
pub(super) source: serde_path_to_error::Error<serde_json::Error>,
}
#[derive(Debug, thiserror::Error)]
#[error(
"The `Content-Type` header was set to `{actual}`. This endpoint expects requests with a `Content-Type` header set to `application/json`, or another `application/*+json` MIME type"
)]
#[non_exhaustive]
pub struct JsonContentTypeMismatch {
pub actual: String,
}
#[derive(Debug, thiserror::Error)]
#[error(
"The `Content-Type` header is missing. This endpoint expects requests with a `Content-Type` header set to `application/x-www-form-urlencoded`"
)]
#[non_exhaustive]
pub struct MissingUrlEncodedContentType;
#[derive(Debug, thiserror::Error)]
#[error(
"The `Content-Type` header was set to `{actual}`. This endpoint expects requests with a `Content-Type` header set to `application/x-www-form-urlencoded`"
)]
#[non_exhaustive]
pub struct UrlEncodedContentTypeMismatch {
pub actual: String,
}
#[derive(Debug, thiserror::Error)]
#[error("Failed to deserialize the body as a urlencoded form.\n{source}")]
#[non_exhaustive]
pub struct UrlEncodedBodyDeserializationError {
#[source]
pub(super) source: serde_html_form::de::Error,
}