Skip to main content

axum_error_sets/
codes.rs

1//! Defines all HTTP status code wrappers.
2
3use super::*;
4
5macro_rules! define_codes {
6(
7    $(
8        $(#[$meta:meta])*
9        $(fn $fn_name:ident ();)?
10        code $name:ident => $status:expr;
11    )*
12) => {
13    $(
14        $(#[$meta])*
15        #[derive(Debug, Clone, Copy, Default)]
16        pub struct $name<T = ()>(pub T);
17
18        impl<T> StatusProvider for $name<T> {
19            const STATUS_CODE: StatusCode = $status;
20            type Inner = T;
21            type WithInner<R> = $name<R>;
22            fn into_inner(self) -> Self::Inner {
23                self.0
24            }
25        }
26
27        impl<T, S> From<$name<T>> for ApiResponse<S>
28        where
29            S: Contains<$name<T>>,
30            $name<T>: StatusProvider<Inner: IntoResponse>,
31        {
32            fn from(err: $name<T>) -> Self {
33                ApiResponse::new(err)
34            }
35        }
36
37        impl<T> From<T> for $name<T> {
38            fn from(inner: T) -> Self {
39                Self(inner)
40            }
41        }
42
43        impl<T> std::ops::Deref for $name<T> {
44            type Target = T;
45
46            fn deref(&self) -> &Self::Target {
47                &self.0
48            }
49        }
50
51        impl<T> std::ops::DerefMut for $name<T> {
52            fn deref_mut(&mut self) -> &mut Self::Target {
53                &mut self.0
54            }
55        }
56    )*
57};
58}
59
60define_codes!(
61    /// 400 Bad Request
62    fn map_bad_request();
63    code BadRequest => StatusCode::BAD_REQUEST;
64
65    /// 401 Unauthorized
66    fn map_unauthorized();
67    code Unauthorized => StatusCode::UNAUTHORIZED;
68
69    /// 402 Payment Required
70    fn map_payment_required();
71    code PaymentRequired => StatusCode::PAYMENT_REQUIRED;
72
73    /// 403 Forbidden
74    fn map_forbidden();
75    code Forbidden => StatusCode::FORBIDDEN;
76
77    /// 404 Not Found
78    fn map_not_found();
79    code NotFound => StatusCode::NOT_FOUND;
80
81    /// 405 Method Not Allowed
82    fn map_method_not_allowed();
83    code MethodNotAllowed => StatusCode::METHOD_NOT_ALLOWED;
84
85    /// 406 Not Acceptable
86    fn map_not_acceptable();
87    code NotAcceptable => StatusCode::NOT_ACCEPTABLE;
88
89    /// 407 Proxy Authentication Required
90    fn map_proxy_authentication_required();
91    code ProxyAuthenticationRequired => StatusCode::PROXY_AUTHENTICATION_REQUIRED;
92
93    /// 408 Request Timeout
94    fn map_request_timeout();
95    code RequestTimeout => StatusCode::REQUEST_TIMEOUT;
96
97    /// 409 Conflict
98    fn map_conflict();
99    code Conflict => StatusCode::CONFLICT;
100
101    /// 410 Gone
102    fn map_gone();
103    code Gone => StatusCode::GONE;
104
105    /// 411 Length Required
106    fn map_length_required();
107    code LengthRequired => StatusCode::LENGTH_REQUIRED;
108
109    /// 412 Precondition Failed
110    fn map_precondition_failed();
111    code PreconditionFailed => StatusCode::PRECONDITION_FAILED;
112
113    /// 413 Payload Too Large
114    fn map_payload_too_large();
115    code PayloadTooLarge => StatusCode::PAYLOAD_TOO_LARGE;
116
117    /// 414 URI Too Long
118    fn map_uri_too_long();
119    code UriTooLong => StatusCode::URI_TOO_LONG;
120
121    /// 415 Unsupported Media Type
122    fn map_unsupported_media_type();
123    code UnsupportedMediaType => StatusCode::UNSUPPORTED_MEDIA_TYPE;
124
125    /// 416 Range Not Satisfiable
126    fn map_range_not_satisfiable();
127    code RangeNotSatisfiable => StatusCode::RANGE_NOT_SATISFIABLE;
128
129    /// 417 Expectation Failed
130    fn map_expectation_failed();
131    code ExpectationFailed => StatusCode::EXPECTATION_FAILED;
132
133    /// 418 I'm a teapot
134    fn map_im_a_teapot();
135    code ImATeapot => StatusCode::IM_A_TEAPOT;
136
137    /// 421 Misdirected Request
138    fn map_misdirected_request();
139    code MisdirectedRequest => StatusCode::MISDIRECTED_REQUEST;
140
141    /// 422 Unprocessable Entity
142    fn map_unprocessable_entity();
143    code UnprocessableEntity => StatusCode::UNPROCESSABLE_ENTITY;
144
145    /// 423 Locked
146    fn map_locked();
147    code Locked => StatusCode::LOCKED;
148
149    /// 424 Failed Dependency
150    fn map_failed_dependency();
151    code FailedDependency => StatusCode::FAILED_DEPENDENCY;
152
153    /// 425 Too Early
154    fn map_too_early();
155    code TooEarly => StatusCode::TOO_EARLY;
156
157    /// 426 Upgrade Required
158    fn map_upgrade_required();
159    code UpgradeRequired => StatusCode::UPGRADE_REQUIRED;
160
161    /// 428 Precondition Required
162    fn map_precondition_required();
163    code PreconditionRequired => StatusCode::PRECONDITION_REQUIRED;
164
165    /// 429 Too Many Requests
166    fn map_too_many_requests();
167    code TooManyRequests => StatusCode::TOO_MANY_REQUESTS;
168
169    /// 431 Request Header Fields Too Large
170    fn map_request_header_fields_too_large();
171    code RequestHeaderFieldsTooLarge => StatusCode::REQUEST_HEADER_FIELDS_TOO_LARGE;
172
173    /// 451 Unavailable For Legal Reasons
174    fn map_unavailable_for_legal_reasons();
175    code UnavailableForLegalReasons => StatusCode::UNAVAILABLE_FOR_LEGAL_REASONS;
176
177    /// 500 Internal Server Error
178    fn map_internal();
179    code Internal => StatusCode::INTERNAL_SERVER_ERROR;
180
181    /// 501 Not Implemented
182    fn map_not_implemented();
183    code NotImplemented => StatusCode::NOT_IMPLEMENTED;
184
185    /// 502 Bad Gateway
186    fn map_bad_gateway();
187    code BadGateway => StatusCode::BAD_GATEWAY;
188
189    /// 503 Service Unavailable
190    fn map_service_unavailable();
191    code ServiceUnavailable => StatusCode::SERVICE_UNAVAILABLE;
192
193    /// 504 Gateway Timeout
194    fn map_gateway_timeout();
195    code GatewayTimeout => StatusCode::GATEWAY_TIMEOUT;
196
197    /// 505 HTTP Version Not Supported
198    fn map_http_version_not_supported();
199    code HttpVersionNotSupported => StatusCode::HTTP_VERSION_NOT_SUPPORTED;
200
201    /// 506 Variant Also Negotiates
202    fn map_variant_also_negotiates();
203    code VariantAlsoNegotiates => StatusCode::VARIANT_ALSO_NEGOTIATES;
204
205    /// 507 Insufficient Storage
206    fn map_insufficient_storage();
207    code InsufficientStorage => StatusCode::INSUFFICIENT_STORAGE;
208
209    /// 508 Loop Detected
210    fn map_loop_detected();
211    code LoopDetected => StatusCode::LOOP_DETECTED;
212
213    /// 510 Not Extended
214    fn map_not_extended();
215    code NotExtended => StatusCode::NOT_EXTENDED;
216
217    /// 511 Network Authentication Required
218    fn map_network_authentication_required();
219    code NetworkAuthenticationRequired => StatusCode::NETWORK_AUTHENTICATION_REQUIRED;
220);