Skip to content

Commit 8e811bb

Browse files
committed
refactor: remove error status reference
1 parent c67edc7 commit 8e811bb

File tree

2 files changed

+53
-31
lines changed

2 files changed

+53
-31
lines changed

src/error/macros.rs

+27-27
Original file line numberDiff line numberDiff line change
@@ -15,143 +15,143 @@ macro_rules! bail {
1515
}
1616

1717
// 4xx
18-
/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::BAD_REQUEST`] from a string or existing non-anyhow error value.
18+
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::BAD_REQUEST`] from a string or existing non-anyhow error value.
1919
#[macro_export]
2020
macro_rules! http_error_bad_request {
2121
($($arg:tt)*) => {{
2222
$crate::error!($($arg)*).with_status(StatusCode::BAD_REQUEST)
2323
}}
2424
}
2525

26-
/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::UNAUTHORIZED`] from a string or existing non-anyhow error value.
26+
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::UNAUTHORIZED`] from a string or existing non-anyhow error value.
2727
#[macro_export]
2828
macro_rules! http_error_unauthorized {
2929
($($arg:tt)*) => {{
3030
$crate::error!($($arg)*).with_status(StatusCode::UNAUTHORIZED)
3131
}}
3232
}
3333

34-
/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::PAYMENT_REQUIRED`] from a string or existing non-anyhow error value.
34+
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::PAYMENT_REQUIRED`] from a string or existing non-anyhow error value.
3535
#[macro_export]
3636
macro_rules! http_error_payment_required {
3737
($($arg:tt)*) => {{
3838
$crate::error!($($arg)*).with_status(StatusCode::PAYMENT_REQUIRED)
3939
}}
4040
}
4141

42-
/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::FORBIDDEN`] from a string or existing non-anyhow error value.
42+
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::FORBIDDEN`] from a string or existing non-anyhow error value.
4343
#[macro_export]
4444
macro_rules! http_error_forbidden {
4545
($($arg:tt)*) => {{
4646
$crate::error!($($arg)*).with_status(StatusCode::FORBIDDEN)
4747
}}
4848
}
4949

50-
/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::NOT_FOUND`] from a string or existing non-anyhow error value.
50+
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::NOT_FOUND`] from a string or existing non-anyhow error value.
5151
#[macro_export]
5252
macro_rules! http_error_not_found {
5353
($($arg:tt)*) => {{
5454
$crate::error!($($arg)*).with_status(StatusCode::NOT_FOUND)
5555
}}
5656
}
5757

58-
/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::METHOD_NOT_ALLOWED`] from a string or existing non-anyhow error value.
58+
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::METHOD_NOT_ALLOWED`] from a string or existing non-anyhow error value.
5959
#[macro_export]
6060
macro_rules! http_error_method_not_allowed {
6161
($($arg:tt)*) => {{
6262
$crate::error!($($arg)*).with_status(StatusCode::METHOD_NOT_ALLOWED)
6363
}}
6464
}
6565

66-
/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::NOT_ACCEPTABLE`] from a string or existing non-anyhow error value.
66+
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::NOT_ACCEPTABLE`] from a string or existing non-anyhow error value.
6767
#[macro_export]
6868
macro_rules! http_error_not_acceptable {
6969
($($arg:tt)*) => {{
7070
$crate::error!($($arg)*).with_status(StatusCode::NOT_ACCEPTABLE)
7171
}}
7272
}
7373

74-
/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::PROXY_AUTHENTICATION_REQUIRED`] from a string or existing non-anyhow error value.
74+
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::PROXY_AUTHENTICATION_REQUIRED`] from a string or existing non-anyhow error value.
7575
#[macro_export]
7676
macro_rules! http_error_proxy_authentication_required {
7777
($($arg:tt)*) => {{
7878
$crate::error!($($arg)*).with_status(StatusCode::PROXY_AUTHENTICATION_REQUIRED)
7979
}}
8080
}
8181

82-
/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::REQUEST_TIMEOUT`] from a string or existing non-anyhow error value.
82+
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::REQUEST_TIMEOUT`] from a string or existing non-anyhow error value.
8383
#[macro_export]
8484
macro_rules! http_error_request_timeout {
8585
($($arg:tt)*) => {{
8686
$crate::error!($($arg)*).with_status(StatusCode::REQUEST_TIMEOUT)
8787
}}
8888
}
8989

90-
/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::CONFLICT`] from a string or existing non-anyhow error value.
90+
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::CONFLICT`] from a string or existing non-anyhow error value.
9191
#[macro_export]
9292
macro_rules! http_error_conflict {
9393
($($arg:tt)*) => {{
9494
$crate::error!($($arg)*).with_status(StatusCode::CONFLICT)
9595
}}
9696
}
9797

98-
/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::GONE`] from a string or existing non-anyhow error value.
98+
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::GONE`] from a string or existing non-anyhow error value.
9999
#[macro_export]
100100
macro_rules! http_error_gone {
101101
($($arg:tt)*) => {{
102102
$crate::error!($($arg)*).with_status(StatusCode::GONE)
103103
}}
104104
}
105105

106-
/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::LENGTH_REQUIRED`] from a string or existing non-anyhow error value.
106+
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::LENGTH_REQUIRED`] from a string or existing non-anyhow error value.
107107
#[macro_export]
108108
macro_rules! http_error_length_required {
109109
($($arg:tt)*) => {{
110110
$crate::error!($($arg)*).with_status(StatusCode::LENGTH_REQUIRED)
111111
}}
112112
}
113113

114-
/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::PRECONDITION_FAILED`] from a string or existing non-anyhow error value.
114+
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::PRECONDITION_FAILED`] from a string or existing non-anyhow error value.
115115
#[macro_export]
116116
macro_rules! http_error_precondition_failed {
117117
($($arg:tt)*) => {{
118118
$crate::error!($($arg)*).with_status(StatusCode::PRECONDITION_FAILED)
119119
}}
120120
}
121121

122-
/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::PAYLOAD_TOO_LARGE`] from a string or existing non-anyhow error value.
122+
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::PAYLOAD_TOO_LARGE`] from a string or existing non-anyhow error value.
123123
#[macro_export]
124124
macro_rules! http_error_payload_too_large {
125125
($($arg:tt)*) => {{
126126
$crate::error!($($arg)*).with_status(StatusCode::PAYLOAD_TOO_LARGE)
127127
}}
128128
}
129129

130-
/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::URI_TOO_LONG`] from a string or existing non-anyhow error value.
130+
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::URI_TOO_LONG`] from a string or existing non-anyhow error value.
131131
#[macro_export]
132132
macro_rules! http_error_uri_too_long {
133133
($($arg:tt)*) => {{
134134
$crate::error!($($arg)*).with_status(StatusCode::URI_TOO_LONG)
135135
}}
136136
}
137137

138-
/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::UNSUPPORTED_MEDIA_TYPE`] from a string or existing non-anyhow error value.
138+
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::UNSUPPORTED_MEDIA_TYPE`] from a string or existing non-anyhow error value.
139139
#[macro_export]
140140
macro_rules! http_error_unsupported_media_type {
141141
($($arg:tt)*) => {{
142142
$crate::error!($($arg)*).with_status(StatusCode::UNSUPPORTED_MEDIA_TYPE)
143143
}}
144144
}
145145

146-
/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::RANGE_NOT_SATISFIABLE`] from a string or existing non-anyhow error value.
146+
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::RANGE_NOT_SATISFIABLE`] from a string or existing non-anyhow error value.
147147
#[macro_export]
148148
macro_rules! http_error_range_not_satisfiable {
149149
($($arg:tt)*) => {{
150150
$crate::error!($($arg)*).with_status(StatusCode::RANGE_NOT_SATISFIABLE)
151151
}}
152152
}
153153

154-
/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::EXPECTATION_FAILED`] from a string or existing non-anyhow error value.
154+
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::EXPECTATION_FAILED`] from a string or existing non-anyhow error value.
155155
#[macro_export]
156156
macro_rules! http_error_expectation_failed {
157157
($($arg:tt)*) => {{
@@ -160,71 +160,71 @@ macro_rules! http_error_expectation_failed {
160160
}
161161

162162
// 50x
163-
/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::INTERNAL_SERVER_ERROR`] from a string or existing non-anyhow error value.
163+
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::INTERNAL_SERVER_ERROR`] from a string or existing non-anyhow error value.
164164
#[macro_export]
165165
macro_rules! http_error_internal_server_error {
166166
($($arg:tt)*) => {{
167167
$crate::error!($($arg)*).with_status(StatusCode::INTERNAL_SERVER_ERROR)
168168
}}
169169
}
170170

171-
/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::NOT_IMPLEMENTED`] from a string or existing non-anyhow error value.
171+
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::NOT_IMPLEMENTED`] from a string or existing non-anyhow error value.
172172
#[macro_export]
173173
macro_rules! http_error_not_implemented {
174174
($($arg:tt)*) => {{
175175
$crate::error!($($arg)*).with_status(StatusCode::NOT_IMPLEMENTED)
176176
}}
177177
}
178178

179-
/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::BAD_GATEWAY`] from a string or existing non-anyhow error value.
179+
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::BAD_GATEWAY`] from a string or existing non-anyhow error value.
180180
#[macro_export]
181181
macro_rules! http_error_bad_gateway {
182182
($($arg:tt)*) => {{
183183
$crate::error!($($arg)*).with_status(StatusCode::BAD_GATEWAY)
184184
}}
185185
}
186186

187-
/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::SERVICE_UNAVAILABLE`] from a string or existing non-anyhow error value.
187+
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::SERVICE_UNAVAILABLE`] from a string or existing non-anyhow error value.
188188
#[macro_export]
189189
macro_rules! http_error_service_unavailable {
190190
($($arg:tt)*) => {{
191191
$crate::error!($($arg)*).with_status(StatusCode::SERVICE_UNAVAILABLE)
192192
}}
193193
}
194194

195-
/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::GATEWAY_TIMEOUT`] from a string or existing non-anyhow error value.
195+
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::GATEWAY_TIMEOUT`] from a string or existing non-anyhow error value.
196196
#[macro_export]
197197
macro_rules! http_error_gateway_timeout {
198198
($($arg:tt)*) => {{
199199
$crate::error!($($arg)*).with_status(StatusCode::GATEWAY_TIMEOUT)
200200
}}
201201
}
202202

203-
/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::HTTP_VERSION_NOT_SUPPORTED`] from a string or existing non-anyhow error value.
203+
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::HTTP_VERSION_NOT_SUPPORTED`] from a string or existing non-anyhow error value.
204204
#[macro_export]
205205
macro_rules! http_error_http_version_not_supported {
206206
($($arg:tt)*) => {{
207207
$crate::error!($($arg)*).with_status(StatusCode::HTTP_VERSION_NOT_SUPPORTED)
208208
}}
209209
}
210210

211-
/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::VARIANT_ALSO_NEGOTIATES`] from a string or existing non-anyhow error value.
211+
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::VARIANT_ALSO_NEGOTIATES`] from a string or existing non-anyhow error value.
212212
#[macro_export]
213213
macro_rules! http_error_variant_also_negotiates {
214214
($($arg:tt)*) => {{
215215
$crate::error!($($arg)*).with_status(StatusCode::VARIANT_ALSO_NEGOTIATES)
216216
}}
217217
}
218218

219-
/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::INSUFFICIENT_STORAGE`] from a string or existing non-anyhow error value.
219+
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::INSUFFICIENT_STORAGE`] from a string or existing non-anyhow error value.
220220
#[macro_export]
221221
macro_rules! http_error_insufficient_storage {
222222
($($arg:tt)*) => {{
223223
$crate::error!($($arg)*).with_status(StatusCode::INSUFFICIENT_STORAGE)
224224
}}
225225
}
226226

227-
/// Constructs an [`Error`][`super::Error`] with [`http::StatusCode::LOOP_DETECTED`] from a string or existing non-anyhow error value.
227+
/// Constructs an [`Error`][`super::Error`] with [`hyper::StatusCode::LOOP_DETECTED`] from a string or existing non-anyhow error value.
228228
#[macro_export]
229229
macro_rules! http_error_loop_detected {
230230
($($arg:tt)*) => {{

src/error/mod.rs

+26-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,30 @@
44
//! The `Error` type implements other several common error types
55
//! to ease conversions while consuming the input value via the [`From`] trait.
66
//!
7-
//! Additionally, when used in HTTP contexts, the `Error` type can be associated to an [HTTP Status Code][`http::StatusCode`]
7+
//! Additionally, when used in HTTP contexts, the `Error` type can be associated to an [HTTP Status Code][`hyper::StatusCode`].
88
//! via the [`Error::with_status`][`super::Error::with_status`] method.
9+
//!
10+
//! a. Construct an [`Error`][`super::Error`] from [`hyper::Error`], [`std::io::Error`], [`anyhow::Error`] or an string.
11+
//!
12+
//! ```rust
13+
//! use hyper_middleware::error
14+
//!
15+
//! let err = Error::from("some error type or string");
16+
//! // Or using a shortcut macro
17+
//! let err = error!("some error type or string");
18+
//! ```
19+
//!
20+
//! b. Construct an [`Error`][`super::Error`] with an associated [HTTP Status Code][`hyper::StatusCode`].
21+
//!
22+
//! ```rust
23+
//! use hyper::StatusCode;
24+
//! use hyper_middleware::error
25+
//!
26+
//! let err = error!("user or password does not match").with_status(StatusCode::UNAUTHORIZED);
27+
//! // Or using a shortcut macro
28+
//! let err = http_error_unauthorized!("user or password does not match");
29+
//! ```
30+
//!
931
1032
use hyper::StatusCode;
1133
use std::fmt;
@@ -29,9 +51,9 @@ impl Error {
2951
self.source
3052
}
3153

32-
/// Returns an HTTP Status Code reference associated with the underlying error.
33-
pub fn status(&self) -> Option<&StatusCode> {
34-
self.status.as_ref()
54+
/// Returns the HTTP `StatusCode` associated with the underlying error.
55+
pub fn status(&self) -> Option<StatusCode> {
56+
self.status
3557
}
3658

3759
/// Adds/updates the current HTTP Status Code.

0 commit comments

Comments
 (0)