hyper_middleware/error/
macros.rs

1/// Constructs an [`Error`][`super::Error`] with the given arguments.
2#[macro_export]
3macro_rules! error {
4    ($($arg:tt)*) => {{
5        $crate::Error::from(anyhow::anyhow!($($arg)*))
6    }}
7}
8
9/// Constructs an [`Error`][`super::Error`] with the given arguments and returns early with an [`Err`] result.
10#[macro_export]
11macro_rules! bail {
12    ($($arg:tt)*) => {{
13        return Err($crate::error!($($arg)*))
14    }}
15}
16
17// 4xx
18/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::BAD_REQUEST`] from a string or existing non-anyhow error value.
19#[macro_export]
20macro_rules! http_error_bad_request {
21    ($($arg:tt)*) => {{
22        $crate::error!($($arg)*).with_status(StatusCode::BAD_REQUEST)
23    }}
24}
25
26/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::UNAUTHORIZED`] from a string or existing non-anyhow error value.
27#[macro_export]
28macro_rules! http_error_unauthorized {
29    ($($arg:tt)*) => {{
30        $crate::error!($($arg)*).with_status(StatusCode::UNAUTHORIZED)
31    }}
32}
33
34/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::PAYMENT_REQUIRED`] from a string or existing non-anyhow error value.
35#[macro_export]
36macro_rules! http_error_payment_required {
37    ($($arg:tt)*) => {{
38        $crate::error!($($arg)*).with_status(StatusCode::PAYMENT_REQUIRED)
39    }}
40}
41
42/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::FORBIDDEN`] from a string or existing non-anyhow error value.
43#[macro_export]
44macro_rules! http_error_forbidden {
45    ($($arg:tt)*) => {{
46        $crate::error!($($arg)*).with_status(StatusCode::FORBIDDEN)
47    }}
48}
49
50/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::NOT_FOUND`] from a string or existing non-anyhow error value.
51#[macro_export]
52macro_rules! http_error_not_found {
53    ($($arg:tt)*) => {{
54        $crate::error!($($arg)*).with_status(StatusCode::NOT_FOUND)
55    }}
56}
57
58/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::METHOD_NOT_ALLOWED`] from a string or existing non-anyhow error value.
59#[macro_export]
60macro_rules! http_error_method_not_allowed {
61    ($($arg:tt)*) => {{
62        $crate::error!($($arg)*).with_status(StatusCode::METHOD_NOT_ALLOWED)
63    }}
64}
65
66/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::NOT_ACCEPTABLE`] from a string or existing non-anyhow error value.
67#[macro_export]
68macro_rules! http_error_not_acceptable {
69    ($($arg:tt)*) => {{
70        $crate::error!($($arg)*).with_status(StatusCode::NOT_ACCEPTABLE)
71    }}
72}
73
74/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::PROXY_AUTHENTICATION_REQUIRED`] from a string or existing non-anyhow error value.
75#[macro_export]
76macro_rules! http_error_proxy_authentication_required {
77    ($($arg:tt)*) => {{
78        $crate::error!($($arg)*).with_status(StatusCode::PROXY_AUTHENTICATION_REQUIRED)
79    }}
80}
81
82/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::REQUEST_TIMEOUT`] from a string or existing non-anyhow error value.
83#[macro_export]
84macro_rules! http_error_request_timeout {
85    ($($arg:tt)*) => {{
86        $crate::error!($($arg)*).with_status(StatusCode::REQUEST_TIMEOUT)
87    }}
88}
89
90/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::CONFLICT`] from a string or existing non-anyhow error value.
91#[macro_export]
92macro_rules! http_error_conflict {
93    ($($arg:tt)*) => {{
94        $crate::error!($($arg)*).with_status(StatusCode::CONFLICT)
95    }}
96}
97
98/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::GONE`] from a string or existing non-anyhow error value.
99#[macro_export]
100macro_rules! http_error_gone {
101    ($($arg:tt)*) => {{
102        $crate::error!($($arg)*).with_status(StatusCode::GONE)
103    }}
104}
105
106/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::LENGTH_REQUIRED`] from a string or existing non-anyhow error value.
107#[macro_export]
108macro_rules! http_error_length_required {
109    ($($arg:tt)*) => {{
110        $crate::error!($($arg)*).with_status(StatusCode::LENGTH_REQUIRED)
111    }}
112}
113
114/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::PRECONDITION_FAILED`] from a string or existing non-anyhow error value.
115#[macro_export]
116macro_rules! http_error_precondition_failed {
117    ($($arg:tt)*) => {{
118        $crate::error!($($arg)*).with_status(StatusCode::PRECONDITION_FAILED)
119    }}
120}
121
122/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::PAYLOAD_TOO_LARGE`] from a string or existing non-anyhow error value.
123#[macro_export]
124macro_rules! http_error_payload_too_large {
125    ($($arg:tt)*) => {{
126        $crate::error!($($arg)*).with_status(StatusCode::PAYLOAD_TOO_LARGE)
127    }}
128}
129
130/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::URI_TOO_LONG`] from a string or existing non-anyhow error value.
131#[macro_export]
132macro_rules! http_error_uri_too_long {
133    ($($arg:tt)*) => {{
134        $crate::error!($($arg)*).with_status(StatusCode::URI_TOO_LONG)
135    }}
136}
137
138/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::UNSUPPORTED_MEDIA_TYPE`] from a string or existing non-anyhow error value.
139#[macro_export]
140macro_rules! http_error_unsupported_media_type {
141    ($($arg:tt)*) => {{
142        $crate::error!($($arg)*).with_status(StatusCode::UNSUPPORTED_MEDIA_TYPE)
143    }}
144}
145
146/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::RANGE_NOT_SATISFIABLE`] from a string or existing non-anyhow error value.
147#[macro_export]
148macro_rules! http_error_range_not_satisfiable {
149    ($($arg:tt)*) => {{
150        $crate::error!($($arg)*).with_status(StatusCode::RANGE_NOT_SATISFIABLE)
151    }}
152}
153
154/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::EXPECTATION_FAILED`] from a string or existing non-anyhow error value.
155#[macro_export]
156macro_rules! http_error_expectation_failed {
157    ($($arg:tt)*) => {{
158        $crate::error!($($arg)*).with_status(StatusCode::EXPECTATION_FAILED)
159    }}
160}
161
162//  50x
163/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::INTERNAL_SERVER_ERROR`] from a string or existing non-anyhow error value.
164#[macro_export]
165macro_rules! http_error_internal_server_error {
166    ($($arg:tt)*) => {{
167        $crate::error!($($arg)*).with_status(StatusCode::INTERNAL_SERVER_ERROR)
168    }}
169}
170
171/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::NOT_IMPLEMENTED`] from a string or existing non-anyhow error value.
172#[macro_export]
173macro_rules! http_error_not_implemented {
174    ($($arg:tt)*) => {{
175        $crate::error!($($arg)*).with_status(StatusCode::NOT_IMPLEMENTED)
176    }}
177}
178
179/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::BAD_GATEWAY`] from a string or existing non-anyhow error value.
180#[macro_export]
181macro_rules! http_error_bad_gateway {
182    ($($arg:tt)*) => {{
183        $crate::error!($($arg)*).with_status(StatusCode::BAD_GATEWAY)
184    }}
185}
186
187/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::SERVICE_UNAVAILABLE`] from a string or existing non-anyhow error value.
188#[macro_export]
189macro_rules! http_error_service_unavailable {
190    ($($arg:tt)*) => {{
191        $crate::error!($($arg)*).with_status(StatusCode::SERVICE_UNAVAILABLE)
192    }}
193}
194
195/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::GATEWAY_TIMEOUT`] from a string or existing non-anyhow error value.
196#[macro_export]
197macro_rules! http_error_gateway_timeout {
198    ($($arg:tt)*) => {{
199        $crate::error!($($arg)*).with_status(StatusCode::GATEWAY_TIMEOUT)
200    }}
201}
202
203/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::HTTP_VERSION_NOT_SUPPORTED`] from a string or existing non-anyhow error value.
204#[macro_export]
205macro_rules! http_error_http_version_not_supported {
206    ($($arg:tt)*) => {{
207        $crate::error!($($arg)*).with_status(StatusCode::HTTP_VERSION_NOT_SUPPORTED)
208    }}
209}
210
211/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::VARIANT_ALSO_NEGOTIATES`] from a string or existing non-anyhow error value.
212#[macro_export]
213macro_rules! http_error_variant_also_negotiates {
214    ($($arg:tt)*) => {{
215        $crate::error!($($arg)*).with_status(StatusCode::VARIANT_ALSO_NEGOTIATES)
216    }}
217}
218
219/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::INSUFFICIENT_STORAGE`] from a string or existing non-anyhow error value.
220#[macro_export]
221macro_rules! http_error_insufficient_storage {
222    ($($arg:tt)*) => {{
223        $crate::error!($($arg)*).with_status(StatusCode::INSUFFICIENT_STORAGE)
224    }}
225}
226
227/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::LOOP_DETECTED`] from a string or existing non-anyhow error value.
228#[macro_export]
229macro_rules! http_error_loop_detected {
230    ($($arg:tt)*) => {{
231        $crate::error!($($arg)*).with_status(StatusCode::LOOP_DETECTED)
232    }}
233}