Skip to main content

axum_error_sets/
codes.rs

1use super::*;
2
3macro_rules! define_codes {
4(
5    $(
6        $(#[$meta:meta])*
7        $(fn $fn_name:ident ();)?
8        code $name:ident => $status:expr;
9    )*
10) => {
11    $(
12        $(#[$meta])*
13        #[derive(Debug, Clone, Copy, Default)]
14        pub struct $name<T = ()>(pub T);
15
16        impl<T> WrapsResponse for $name<T> {
17            const STATUS_CODE: StatusCode = $status;
18            type Inner = T;
19            type Pure = $name;
20            fn into_inner(self) -> Self::Inner {
21                self.0
22            }
23        }
24
25        impl<T, S> From<$name<T>> for ApiResponse<S>
26        where
27            S: Contains<$name<T>>,
28            $name<T>: WrapsResponse<Inner: IntoResponse>,
29        {
30            fn from(err: $name<T>) -> Self {
31                ApiResponse::new(err)
32            }
33        }
34
35        impl<T> From<T> for $name<T> {
36            fn from(inner: T) -> Self {
37                Self(inner)
38            }
39        }
40
41        impl<T> std::ops::Deref for $name<T> {
42            type Target = T;
43
44            fn deref(&self) -> &Self::Target {
45                &self.0
46            }
47        }
48
49        impl<T> std::ops::DerefMut for $name<T> {
50            fn deref_mut(&mut self) -> &mut Self::Target {
51                &mut self.0
52            }
53        }
54    )*
55
56    pub trait ApiResultExt<T, E> {
57        $(
58            $(
59                fn $fn_name<R>(self) -> Result<T, $name<R>>
60                where
61                    E: Into<R>;
62            )?
63        )*
64    }
65
66    impl<T, E> ApiResultExt<T, E> for Result<T, E> {
67        $(
68            $(
69                fn $fn_name<R>(self) -> Result<T, $name<R>>
70                where
71                    E: Into<R>,
72                {
73                    match self {
74                        Ok(val) => Ok(val),
75                        Err(e) => Err($name(e.into())),
76                    }
77                }
78            )?
79        )*
80    }
81};
82}
83
84define_codes!(
85    /// 400 Bad Request
86    fn bad_request();
87    code BadRequest => StatusCode::BAD_REQUEST;
88
89    /// 401 Unauthorized
90    fn unauthorized_err();
91    code Unauthorized => StatusCode::UNAUTHORIZED;
92
93    /// 402 Payment Required
94    fn payment_required_err();
95    code PaymentRequired => StatusCode::PAYMENT_REQUIRED;
96
97    /// 403 Forbidden
98    fn forbidden_err();
99    code Forbidden => StatusCode::FORBIDDEN;
100
101    /// 404 Not Found
102    fn not_found_err();
103    code NotFound => StatusCode::NOT_FOUND;
104
105    /// 405 Method Not Allowed
106    fn method_not_allowed_err();
107    code MethodNotAllowed => StatusCode::METHOD_NOT_ALLOWED;
108
109    /// 406 Not Acceptable
110    fn not_acceptable_err();
111    code NotAcceptable => StatusCode::NOT_ACCEPTABLE;
112
113    /// 407 Proxy Authentication Required
114    fn proxy_authentication_required_err();
115    code ProxyAuthenticationRequired => StatusCode::PROXY_AUTHENTICATION_REQUIRED;
116
117    /// 408 Request Timeout
118    fn request_timeout_err();
119    code RequestTimeout => StatusCode::REQUEST_TIMEOUT;
120
121    /// 409 Conflict
122    fn conflict_err();
123    code Conflict => StatusCode::CONFLICT;
124
125    /// 410 Gone
126    fn gone_err();
127    code Gone => StatusCode::GONE;
128
129    /// 411 Length Required
130    fn length_required_err();
131    code LengthRequired => StatusCode::LENGTH_REQUIRED;
132
133    /// 412 Precondition Failed
134    fn precondition_failed_err();
135    code PreconditionFailed => StatusCode::PRECONDITION_FAILED;
136
137    // /// 413 Content Too Large
138    // code ContentTooLarge => StatusCode::CONTENT_TOO_LARGE;
139
140    /// 414 URI Too Long
141    fn uri_too_long_err();
142    code UriTooLong => StatusCode::URI_TOO_LONG;
143
144    /// 415 Unsupported Media Type
145    fn unsupported_media_type_err();
146    code UnsupportedMediaType => StatusCode::UNSUPPORTED_MEDIA_TYPE;
147
148    /// 416 Range Not Satisfiable
149    fn range_not_satisfiable_err();
150    code RangeNotSatisfiable => StatusCode::RANGE_NOT_SATISFIABLE;
151
152    /// 417 Expectation Failed
153    fn expectation_failed_err();
154    code ExpectationFailed => StatusCode::EXPECTATION_FAILED;
155
156    /// 418 I'm a teapot
157    fn im_a_teapot_err();
158    code ImATeapot => StatusCode::IM_A_TEAPOT;
159
160    /// 421 Misdirected Request
161    fn misdirected_request_err();
162    code MisdirectedRequest => StatusCode::MISDIRECTED_REQUEST;
163
164    /// 422 Unprocessable Entity
165    fn unprocessable_entity_err();
166    code UnprocessableEntity => StatusCode::UNPROCESSABLE_ENTITY;
167
168    /// 423 Locked
169    fn locked_err();
170    code Locked => StatusCode::LOCKED;
171
172    /// 424 Failed Dependency
173    fn failed_dependency_err();
174    code FailedDependency => StatusCode::FAILED_DEPENDENCY;
175
176    // /// 425 Too Early
177    // code TooEarly => StatusCode::TOO_EARLY;
178
179    /// 426 Upgrade Required
180    fn upgrade_required_err();
181    code UpgradeRequired => StatusCode::UPGRADE_REQUIRED;
182
183    /// 428 Precondition Required
184    fn precondition_required_err();
185    code PreconditionRequired => StatusCode::PRECONDITION_REQUIRED;
186
187    /// 429 Too Many Requests
188    fn too_many_requests_err();
189    code TooManyRequests => StatusCode::TOO_MANY_REQUESTS;
190
191    /// 431 Request Header Fields Too Large
192    fn request_header_fields_too_large_err();
193    code RequestHeaderFieldsTooLarge => StatusCode::REQUEST_HEADER_FIELDS_TOO_LARGE;
194
195    /// 451 Unavailable For Legal Reasons
196    fn unavailable_for_legal_reasons_err();
197    code UnavailableForLegalReasons => StatusCode::UNAVAILABLE_FOR_LEGAL_REASONS;
198
199    /// 500 Internal Server Error
200    fn internal_err();
201    code InternalServerError => StatusCode::INTERNAL_SERVER_ERROR;
202
203    /// 501 Not Implemented
204    fn not_implemented_err();
205    code NotImplemented => StatusCode::NOT_IMPLEMENTED;
206
207    /// 502 Bad Gateway
208    fn bad_gateway_err();
209    code BadGateway => StatusCode::BAD_GATEWAY;
210
211    /// 503 Service Unavailable
212    fn service_unavailable_err();
213    code ServiceUnavailable => StatusCode::SERVICE_UNAVAILABLE;
214
215    /// 504 Gateway Timeout
216    fn gateway_timeout_err();
217    code GatewayTimeout => StatusCode::GATEWAY_TIMEOUT;
218
219    /// 505 HTTP Version Not Supported
220    fn http_version_not_supported_err();
221    code HttpVersionNotSupported => StatusCode::HTTP_VERSION_NOT_SUPPORTED;
222
223    /// 506 Variant Also Negotiates
224    fn variant_also_negotiates_err();
225    code VariantAlsoNegotiates => StatusCode::VARIANT_ALSO_NEGOTIATES;
226
227    /// 507 Insufficient Storage
228    fn insufficient_storage_err();
229    code InsufficientStorage => StatusCode::INSUFFICIENT_STORAGE;
230
231    /// 508 Loop Detected
232    fn loop_detected_err();
233    code LoopDetected => StatusCode::LOOP_DETECTED;
234
235    /// 510 Not Extended
236    fn not_extended_err();
237    code NotExtended => StatusCode::NOT_EXTENDED;
238
239    /// 511 Network Authentication Required
240    fn network_authentication_required_err();
241    code NetworkAuthenticationRequired => StatusCode::NETWORK_AUTHENTICATION_REQUIRED;
242);