1 //! Rejection response types. 2 3 use crate::__composite_rejection as composite_rejection; 4 use crate::__define_rejection as define_rejection; 5 6 use crate::BoxError; 7 8 composite_rejection! { 9 /// Rejection type for extractors that buffer the request body. Used if the 10 /// request body cannot be buffered due to an error. 11 pub enum FailedToBufferBody { 12 LengthLimitError, 13 UnknownBodyError, 14 } 15 } 16 17 impl FailedToBufferBody { from_err<E>(err: E) -> Self where E: Into<BoxError>,18 pub(crate) fn from_err<E>(err: E) -> Self 19 where 20 E: Into<BoxError>, 21 { 22 match err.into().downcast::<http_body::LengthLimitError>() { 23 Ok(err) => Self::LengthLimitError(LengthLimitError::from_err(err)), 24 Err(err) => Self::UnknownBodyError(UnknownBodyError::from_err(err)), 25 } 26 } 27 } 28 29 define_rejection! { 30 #[status = PAYLOAD_TOO_LARGE] 31 #[body = "Failed to buffer the request body"] 32 /// Encountered some other error when buffering the body. 33 /// 34 /// This can _only_ happen when you're using [`tower_http::limit::RequestBodyLimitLayer`] or 35 /// otherwise wrapping request bodies in [`http_body::Limited`]. 36 pub struct LengthLimitError(Error); 37 } 38 39 define_rejection! { 40 #[status = BAD_REQUEST] 41 #[body = "Failed to buffer the request body"] 42 /// Encountered an unknown error when buffering the body. 43 pub struct UnknownBodyError(Error); 44 } 45 46 define_rejection! { 47 #[status = BAD_REQUEST] 48 #[body = "Request body didn't contain valid UTF-8"] 49 /// Rejection type used when buffering the request into a [`String`] if the 50 /// body doesn't contain valid UTF-8. 51 pub struct InvalidUtf8(Error); 52 } 53 54 composite_rejection! { 55 /// Rejection used for [`Bytes`](bytes::Bytes). 56 /// 57 /// Contains one variant for each way the [`Bytes`](bytes::Bytes) extractor 58 /// can fail. 59 pub enum BytesRejection { 60 FailedToBufferBody, 61 } 62 } 63 64 composite_rejection! { 65 /// Rejection used for [`String`]. 66 /// 67 /// Contains one variant for each way the [`String`] extractor can fail. 68 pub enum StringRejection { 69 FailedToBufferBody, 70 InvalidUtf8, 71 } 72 } 73