1 //! Rejection response types.
2 
3 use axum_core::__composite_rejection as composite_rejection;
4 use axum_core::__define_rejection as define_rejection;
5 
6 pub use crate::extract::path::{FailedToDeserializePathParams, InvalidUtf8InPathParam};
7 pub use axum_core::extract::rejection::*;
8 
9 #[cfg(feature = "json")]
10 define_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")]
22 define_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")]
33 define_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 
42 define_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 
50 define_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 
59 define_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 
68 define_rejection! {
69     #[status = BAD_REQUEST]
70     #[body = "No host found in request"]
71     /// Rejection type used if the [`Host`](super::Host) extractor is unable to
72     /// resolve a host.
73     pub struct FailedToResolveHost;
74 }
75 
76 define_rejection! {
77     #[status = BAD_REQUEST]
78     #[body = "Failed to deserialize form"]
79     /// Rejection type used if the [`Form`](super::Form) extractor is unable to
80     /// deserialize the form into the target type.
81     pub struct FailedToDeserializeForm(Error);
82 }
83 
84 define_rejection! {
85     #[status = UNPROCESSABLE_ENTITY]
86     #[body = "Failed to deserialize form body"]
87     /// Rejection type used if the [`Form`](super::Form) extractor is unable to
88     /// deserialize the form body into the target type.
89     pub struct FailedToDeserializeFormBody(Error);
90 }
91 
92 define_rejection! {
93     #[status = BAD_REQUEST]
94     #[body = "Failed to deserialize query string"]
95     /// Rejection type used if the [`Query`](super::Query) extractor is unable to
96     /// deserialize the query string into the target type.
97     pub struct FailedToDeserializeQueryString(Error);
98 }
99 
100 composite_rejection! {
101     /// Rejection used for [`Query`](super::Query).
102     ///
103     /// Contains one variant for each way the [`Query`](super::Query) extractor
104     /// can fail.
105     pub enum QueryRejection {
106         FailedToDeserializeQueryString,
107     }
108 }
109 
110 composite_rejection! {
111     /// Rejection used for [`Form`](super::Form).
112     ///
113     /// Contains one variant for each way the [`Form`](super::Form) extractor
114     /// can fail.
115     pub enum FormRejection {
116         InvalidFormContentType,
117         FailedToDeserializeForm,
118         FailedToDeserializeFormBody,
119         BytesRejection,
120     }
121 }
122 
123 composite_rejection! {
124     /// Rejection used for [`RawForm`](super::RawForm).
125     ///
126     /// Contains one variant for each way the [`RawForm`](super::RawForm) extractor
127     /// can fail.
128     pub enum RawFormRejection {
129         InvalidFormContentType,
130         BytesRejection,
131     }
132 }
133 
134 #[cfg(feature = "json")]
135 composite_rejection! {
136     /// Rejection used for [`Json`](super::Json).
137     ///
138     /// Contains one variant for each way the [`Json`](super::Json) extractor
139     /// can fail.
140     #[cfg_attr(docsrs, doc(cfg(feature = "json")))]
141     pub enum JsonRejection {
142         JsonDataError,
143         JsonSyntaxError,
144         MissingJsonContentType,
145         BytesRejection,
146     }
147 }
148 
149 composite_rejection! {
150     /// Rejection used for [`Extension`](super::Extension).
151     ///
152     /// Contains one variant for each way the [`Extension`](super::Extension) extractor
153     /// can fail.
154     pub enum ExtensionRejection {
155         MissingExtension,
156     }
157 }
158 
159 composite_rejection! {
160     /// Rejection used for [`Path`](super::Path).
161     ///
162     /// Contains one variant for each way the [`Path`](super::Path) extractor
163     /// can fail.
164     pub enum PathRejection {
165         FailedToDeserializePathParams,
166         MissingPathParams,
167     }
168 }
169 
170 composite_rejection! {
171     /// Rejection used for [`RawPathParams`](super::RawPathParams).
172     ///
173     /// Contains one variant for each way the [`RawPathParams`](super::RawPathParams) extractor
174     /// can fail.
175     pub enum RawPathParamsRejection {
176         InvalidUtf8InPathParam,
177         MissingPathParams,
178     }
179 }
180 
181 composite_rejection! {
182     /// Rejection used for [`Host`](super::Host).
183     ///
184     /// Contains one variant for each way the [`Host`](super::Host) extractor
185     /// can fail.
186     pub enum HostRejection {
187         FailedToResolveHost,
188     }
189 }
190 
191 #[cfg(feature = "matched-path")]
192 define_rejection! {
193     #[status = INTERNAL_SERVER_ERROR]
194     #[body = "No matched path found"]
195     /// Rejection if no matched path could be found.
196     ///
197     /// See [`MatchedPath`](super::MatchedPath) for more details.
198     #[cfg_attr(docsrs, doc(cfg(feature = "matched-path")))]
199     pub struct MatchedPathMissing;
200 }
201 
202 #[cfg(feature = "matched-path")]
203 composite_rejection! {
204     /// Rejection used for [`MatchedPath`](super::MatchedPath).
205     #[cfg_attr(docsrs, doc(cfg(feature = "matched-path")))]
206     pub enum MatchedPathRejection {
207         MatchedPathMissing,
208     }
209 }
210 
211 #[cfg(feature = "headers")]
212 pub use crate::typed_header::{TypedHeaderRejection, TypedHeaderRejectionReason};
213