1//! Rejection response types.
23use crate::__composite_rejection as composite_rejection;
4use crate::__define_rejection as define_rejection;
56use crate::{BoxError, Error};
78composite_rejection! {
9/// Rejection type for extractors that buffer the request body. Used if the
10 /// request body cannot be buffered due to an error.
11pub enum FailedToBufferBody {
12 LengthLimitError,
13 UnknownBodyError,
14 }
15}
1617impl FailedToBufferBody {
18pub(crate) fn from_err<E>(err: E) -> Self
19where
20E: Into<BoxError>,
21 {
22// two layers of boxes here because `with_limited_body`
23 // wraps the `http_body_util::Limited` in a `axum_core::Body`
24 // which also wraps the error type
25let box_error = match err.into().downcast::<Error>() {
26Ok(err) => err.into_inner(),
27Err(err) => err,
28 };
29let box_error = match box_error.downcast::<Error>() {
30Ok(err) => err.into_inner(),
31Err(err) => err,
32 };
33match box_error.downcast::<http_body_util::LengthLimitError>() {
34Ok(err) => Self::LengthLimitError(LengthLimitError::from_err(err)),
35Err(err) => Self::UnknownBodyError(UnknownBodyError::from_err(err)),
36 }
37 }
38}
3940define_rejection! {
41#[status = PAYLOAD_TOO_LARGE]
42 #[body = "Failed to buffer the request body"]
43/// Encountered some other error when buffering the body.
44 ///
45 /// This can _only_ happen when you're using [`tower_http::limit::RequestBodyLimitLayer`] or
46 /// otherwise wrapping request bodies in [`http_body_util::Limited`].
47pub struct LengthLimitError(Error);
48}
4950define_rejection! {
51#[status = BAD_REQUEST]
52 #[body = "Failed to buffer the request body"]
53/// Encountered an unknown error when buffering the body.
54pub struct UnknownBodyError(Error);
55}
5657define_rejection! {
58#[status = BAD_REQUEST]
59 #[body = "Request body didn't contain valid UTF-8"]
60/// Rejection type used when buffering the request into a [`String`] if the
61 /// body doesn't contain valid UTF-8.
62pub struct InvalidUtf8(Error);
63}
6465composite_rejection! {
66/// Rejection used for [`Bytes`](bytes::Bytes).
67 ///
68 /// Contains one variant for each way the [`Bytes`](bytes::Bytes) extractor
69 /// can fail.
70pub enum BytesRejection {
71 FailedToBufferBody,
72 }
73}
7475composite_rejection! {
76/// Rejection used for [`String`].
77 ///
78 /// Contains one variant for each way the [`String`] extractor can fail.
79pub enum StringRejection {
80 FailedToBufferBody,
81 InvalidUtf8,
82 }
83}