axum/extract/
rejection.rs

1//! Rejection response types.
2
3use axum_core::__composite_rejection as composite_rejection;
4use axum_core::__define_rejection as define_rejection;
5
6pub use crate::extract::path::{FailedToDeserializePathParams, InvalidUtf8InPathParam};
7pub use axum_core::extract::rejection::*;
8
9#[cfg(feature = "json")]
10define_rejection! {
11    #[status = UNPROCESSABLE_ENTITY]
12    #[body = "Failed to deserialize the JSON body into the target type"]
13    #[cfg_attr(docsrs, doc(cfg(feature = "json")))]
14    /// Rejection type for [`Json`](super::Json).
15    ///
16    /// This rejection is used if the request body is syntactically valid JSON but couldn't be
17    /// deserialized into the target type.
18    pub struct JsonDataError(Error);
19}
20
21#[cfg(feature = "json")]
22define_rejection! {
23    #[status = BAD_REQUEST]
24    #[body = "Failed to parse the request body as JSON"]
25    #[cfg_attr(docsrs, doc(cfg(feature = "json")))]
26    /// Rejection type for [`Json`](super::Json).
27    ///
28    /// This rejection is used if the request body didn't contain syntactically valid JSON.
29    pub struct JsonSyntaxError(Error);
30}
31
32#[cfg(feature = "json")]
33define_rejection! {
34    #[status = UNSUPPORTED_MEDIA_TYPE]
35    #[body = "Expected request with `Content-Type: application/json`"]
36    #[cfg_attr(docsrs, doc(cfg(feature = "json")))]
37    /// Rejection type for [`Json`](super::Json) used if the `Content-Type`
38    /// header is missing.
39    pub struct MissingJsonContentType;
40}
41
42define_rejection! {
43    #[status = INTERNAL_SERVER_ERROR]
44    #[body = "Missing request extension"]
45    /// Rejection type for [`Extension`](super::Extension) if an expected
46    /// request extension was not found.
47    pub struct MissingExtension(Error);
48}
49
50define_rejection! {
51    #[status = INTERNAL_SERVER_ERROR]
52    #[body = "No paths parameters found for matched route"]
53    /// Rejection type used if axum's internal representation of path parameters
54    /// is missing. This is commonly caused by extracting `Request<_>`. `Path`
55    /// must be extracted first.
56    pub struct MissingPathParams;
57}
58
59define_rejection! {
60    #[status = UNSUPPORTED_MEDIA_TYPE]
61    #[body = "Form requests must have `Content-Type: application/x-www-form-urlencoded`"]
62    /// Rejection type for [`Form`](super::Form) or [`RawForm`](super::RawForm)
63    /// used if the `Content-Type` header is missing
64    /// or its value is not `application/x-www-form-urlencoded`.
65    pub struct InvalidFormContentType;
66}
67
68define_rejection! {
69    #[status = BAD_REQUEST]
70    #[body = "Failed to deserialize form"]
71    /// Rejection type used if the [`Form`](super::Form) extractor is unable to
72    /// deserialize the form into the target type.
73    pub struct FailedToDeserializeForm(Error);
74}
75
76define_rejection! {
77    #[status = UNPROCESSABLE_ENTITY]
78    #[body = "Failed to deserialize form body"]
79    /// Rejection type used if the [`Form`](super::Form) extractor is unable to
80    /// deserialize the form body into the target type.
81    pub struct FailedToDeserializeFormBody(Error);
82}
83
84define_rejection! {
85    #[status = BAD_REQUEST]
86    #[body = "Failed to deserialize query string"]
87    /// Rejection type used if the [`Query`](super::Query) extractor is unable to
88    /// deserialize the query string into the target type.
89    pub struct FailedToDeserializeQueryString(Error);
90}
91
92composite_rejection! {
93    /// Rejection used for [`Query`](super::Query).
94    ///
95    /// Contains one variant for each way the [`Query`](super::Query) extractor
96    /// can fail.
97    pub enum QueryRejection {
98        FailedToDeserializeQueryString,
99    }
100}
101
102composite_rejection! {
103    /// Rejection used for [`Form`](super::Form).
104    ///
105    /// Contains one variant for each way the [`Form`](super::Form) extractor
106    /// can fail.
107    pub enum FormRejection {
108        InvalidFormContentType,
109        FailedToDeserializeForm,
110        FailedToDeserializeFormBody,
111        BytesRejection,
112    }
113}
114
115composite_rejection! {
116    /// Rejection used for [`RawForm`](super::RawForm).
117    ///
118    /// Contains one variant for each way the [`RawForm`](super::RawForm) extractor
119    /// can fail.
120    pub enum RawFormRejection {
121        InvalidFormContentType,
122        BytesRejection,
123    }
124}
125
126#[cfg(feature = "json")]
127composite_rejection! {
128    /// Rejection used for [`Json`](super::Json).
129    ///
130    /// Contains one variant for each way the [`Json`](super::Json) extractor
131    /// can fail.
132    #[cfg_attr(docsrs, doc(cfg(feature = "json")))]
133    pub enum JsonRejection {
134        JsonDataError,
135        JsonSyntaxError,
136        MissingJsonContentType,
137        BytesRejection,
138    }
139}
140
141composite_rejection! {
142    /// Rejection used for [`Extension`](super::Extension).
143    ///
144    /// Contains one variant for each way the [`Extension`](super::Extension) extractor
145    /// can fail.
146    pub enum ExtensionRejection {
147        MissingExtension,
148    }
149}
150
151composite_rejection! {
152    /// Rejection used for [`Path`](super::Path).
153    ///
154    /// Contains one variant for each way the [`Path`](super::Path) extractor
155    /// can fail.
156    pub enum PathRejection {
157        FailedToDeserializePathParams,
158        MissingPathParams,
159    }
160}
161
162composite_rejection! {
163    /// Rejection used for [`RawPathParams`](super::RawPathParams).
164    ///
165    /// Contains one variant for each way the [`RawPathParams`](super::RawPathParams) extractor
166    /// can fail.
167    pub enum RawPathParamsRejection {
168        InvalidUtf8InPathParam,
169        MissingPathParams,
170    }
171}
172
173#[cfg(feature = "matched-path")]
174define_rejection! {
175    #[status = INTERNAL_SERVER_ERROR]
176    #[body = "No matched path found"]
177    /// Rejection if no matched path could be found.
178    ///
179    /// See [`MatchedPath`](super::MatchedPath) for more details.
180    #[cfg_attr(docsrs, doc(cfg(feature = "matched-path")))]
181    pub struct MatchedPathMissing;
182}
183
184#[cfg(feature = "matched-path")]
185composite_rejection! {
186    /// Rejection used for [`MatchedPath`](super::MatchedPath).
187    #[cfg_attr(docsrs, doc(cfg(feature = "matched-path")))]
188    pub enum MatchedPathRejection {
189        MatchedPathMissing,
190    }
191}
192
193define_rejection! {
194    #[status = INTERNAL_SERVER_ERROR]
195    #[body = "The matched route is not nested"]
196    /// Rejection type for [`NestedPath`](super::NestedPath).
197    ///
198    /// This rejection is used if the matched route wasn't nested.
199    pub struct NestedPathRejection;
200}