Skip to content

Commit 4cdfa1d

Browse files
committed
feat: error macros for all http status codes
1 parent c45e0f7 commit 4cdfa1d

File tree

1 file changed

+242
-22
lines changed

1 file changed

+242
-22
lines changed

src/error.rs

Lines changed: 242 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,16 @@ pub struct Error {
1818
status: Option<StatusCode>,
1919
}
2020

21-
impl fmt::Display for Error {
22-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23-
fmt::Display::fmt(&self.source, f)
24-
}
25-
}
26-
2721
impl Error {
2822
/// Construct an ad-hoc an HTTP `Error` from another error type.
29-
pub fn from_err(error: anyhow::Error, status: Option<StatusCode>) -> Self {
30-
Self {
31-
source: error,
32-
status,
33-
}
23+
pub fn from_err(source: anyhow::Error, status: Option<StatusCode>) -> Self {
24+
Self { source, status }
3425
}
3526

3627
/// Construct an ad-hoc an HTTP `Error` from a string error.
37-
pub fn from_str(error: &str, status: Option<StatusCode>) -> Self {
28+
pub fn from_str(source: &str, status: Option<StatusCode>) -> Self {
3829
Self {
39-
source: anyhow::anyhow!(error.to_owned()),
30+
source: anyhow::anyhow!(source.to_owned()),
4031
status,
4132
}
4233
}
@@ -47,21 +38,32 @@ impl Error {
4738
}
4839

4940
/// Gets the HTTP status code associated with the error.
50-
pub fn status(&self) -> Option<StatusCode> {
51-
self.status
41+
pub fn status(&self) -> Option<&StatusCode> {
42+
self.status.as_ref()
5243
}
44+
}
5345

54-
/// Sets the HTTP status code associated with the error.
55-
pub fn status_mut(&mut self) -> &mut Option<StatusCode> {
56-
&mut self.status
46+
impl fmt::Display for Error {
47+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48+
fmt::Display::fmt(&self.source, f)
5749
}
5850
}
5951

6052
impl From<anyhow::Error> for Error {
61-
/// Converts whatever error into an HTTP `Error`.
62-
fn from(error: anyhow::Error) -> Self {
53+
/// Converts whatever error type into a HTTP `Error`.
54+
fn from(err: anyhow::Error) -> Self {
55+
Self {
56+
source: err,
57+
status: None,
58+
}
59+
}
60+
}
61+
62+
impl From<&str> for Error {
63+
/// Converts a string error into a HTTP `Error`.
64+
fn from(err: &str) -> Self {
6365
Self {
64-
source: error,
66+
source: anyhow::anyhow!(err.to_owned()),
6567
status: None,
6668
}
6769
}
@@ -71,6 +73,224 @@ impl From<anyhow::Error> for Error {
7173
#[macro_export]
7274
macro_rules! error {
7375
($($arg:tt)*) => {{
74-
Error::from(anyhow::anyhow!($($arg)*))
76+
Error::from_err(anyhow::anyhow!($($arg)*), None)
77+
}}
78+
}
79+
80+
// 4xx
81+
/// Construct an `Error` with `StatusCode::BAD_REQUEST` from a string or existing non-anyhow error value.
82+
#[macro_export]
83+
macro_rules! bad_request {
84+
($($arg:tt)*) => {{
85+
Error::from_err(anyhow::anyhow!($($arg)*), Some(StatusCode::BAD_REQUEST))
86+
}}
87+
}
88+
89+
/// Construct an `Error` with `StatusCode::UNAUTHORIZED` from a string or existing non-anyhow error value.
90+
#[macro_export]
91+
macro_rules! unauthorized {
92+
($($arg:tt)*) => {{
93+
Error::from_err(anyhow::anyhow!($($arg)*), Some(StatusCode::UNAUTHORIZED))
94+
}}
95+
}
96+
97+
/// Construct an `Error` with `StatusCode::PAYMENT_REQUIRED` from a string or existing non-anyhow error value.
98+
#[macro_export]
99+
macro_rules! payment_required {
100+
($($arg:tt)*) => {{
101+
Error::from_err(anyhow::anyhow!($($arg)*), Some(StatusCode::PAYMENT_REQUIRED))
102+
}}
103+
}
104+
105+
/// Construct an `Error` with `StatusCode::FORBIDDEN` from a string or existing non-anyhow error value.
106+
#[macro_export]
107+
macro_rules! forbidden {
108+
($($arg:tt)*) => {{
109+
Error::from_err(anyhow::anyhow!($($arg)*), Some(StatusCode::FORBIDDEN))
110+
}}
111+
}
112+
113+
/// Construct an `Error` with `StatusCode::NOT_FOUND` from a string or existing non-anyhow error value.
114+
#[macro_export]
115+
macro_rules! not_found {
116+
($($arg:tt)*) => {{
117+
Error::from_err(anyhow::anyhow!($($arg)*), Some(StatusCode::NOT_FOUND))
118+
}}
119+
}
120+
121+
/// Construct an `Error` with `StatusCode::METHOD_NOT_ALLOWED` from a string or existing non-anyhow error value.
122+
#[macro_export]
123+
macro_rules! method_not_allowed {
124+
($($arg:tt)*) => {{
125+
Error::from_err(anyhow::anyhow!($($arg)*), Some(StatusCode::METHOD_NOT_ALLOWED))
126+
}}
127+
}
128+
129+
/// Construct an `Error` with `StatusCode::NOT_ACCEPTABLE` from a string or existing non-anyhow error value.
130+
#[macro_export]
131+
macro_rules! not_acceptable {
132+
($($arg:tt)*) => {{
133+
Error::from_err(anyhow::anyhow!($($arg)*), Some(StatusCode::NOT_ACCEPTABLE))
134+
}}
135+
}
136+
137+
/// Construct an `Error` with `StatusCode::PROXY_AUTHENTICATION_REQUIRED` from a string or existing non-anyhow error value.
138+
#[macro_export]
139+
macro_rules! proxy_authentication_required {
140+
($($arg:tt)*) => {{
141+
Error::from_err(anyhow::anyhow!($($arg)*), Some(StatusCode::PROXY_AUTHENTICATION_REQUIRED))
142+
}}
143+
}
144+
145+
/// Construct an `Error` with `StatusCode::REQUEST_TIMEOUT` from a string or existing non-anyhow error value.
146+
#[macro_export]
147+
macro_rules! request_timeout {
148+
($($arg:tt)*) => {{
149+
Error::from_err(anyhow::anyhow!($($arg)*), Some(StatusCode::REQUEST_TIMEOUT))
150+
}}
151+
}
152+
153+
/// Construct an `Error` with `StatusCode::CONFLICT` from a string or existing non-anyhow error value.
154+
#[macro_export]
155+
macro_rules! conflict {
156+
($($arg:tt)*) => {{
157+
Error::from_err(anyhow::anyhow!($($arg)*), Some(StatusCode::CONFLICT))
158+
}}
159+
}
160+
161+
/// Construct an `Error` with `StatusCode::GONE` from a string or existing non-anyhow error value.
162+
#[macro_export]
163+
macro_rules! gone {
164+
($($arg:tt)*) => {{
165+
Error::from_err(anyhow::anyhow!($($arg)*), Some(StatusCode::GONE))
166+
}}
167+
}
168+
169+
/// Construct an `Error` with `StatusCode::LENGTH_REQUIRED` from a string or existing non-anyhow error value.
170+
#[macro_export]
171+
macro_rules! length_required {
172+
($($arg:tt)*) => {{
173+
Error::from_err(anyhow::anyhow!($($arg)*), Some(StatusCode::LENGTH_REQUIRED))
174+
}}
175+
}
176+
177+
/// Construct an `Error` with `StatusCode::PRECONDITION_FAILED` from a string or existing non-anyhow error value.
178+
#[macro_export]
179+
macro_rules! precondition_failed {
180+
($($arg:tt)*) => {{
181+
Error::from_err(anyhow::anyhow!($($arg)*), Some(StatusCode::PRECONDITION_FAILED))
182+
}}
183+
}
184+
185+
/// Construct an `Error` with `StatusCode::PAYLOAD_TOO_LARGE` from a string or existing non-anyhow error value.
186+
#[macro_export]
187+
macro_rules! payload_too_large {
188+
($($arg:tt)*) => {{
189+
Error::from_err(anyhow::anyhow!($($arg)*), Some(StatusCode::PAYLOAD_TOO_LARGE))
190+
}}
191+
}
192+
193+
/// Construct an `Error` with `StatusCode::URI_TOO_LONG` from a string or existing non-anyhow error value.
194+
#[macro_export]
195+
macro_rules! uri_too_long {
196+
($($arg:tt)*) => {{
197+
Error::from_err(anyhow::anyhow!($($arg)*), Some(StatusCode::URI_TOO_LONG))
198+
}}
199+
}
200+
201+
/// Construct an `Error` with `StatusCode::UNSUPPORTED_MEDIA_TYPE` from a string or existing non-anyhow error value.
202+
#[macro_export]
203+
macro_rules! unsupported_media_type {
204+
($($arg:tt)*) => {{
205+
Error::from_err(anyhow::anyhow!($($arg)*), Some(StatusCode::UNSUPPORTED_MEDIA_TYPE))
206+
}}
207+
}
208+
209+
/// Construct an `Error` with `StatusCode::RANGE_NOT_SATISFIABLE` from a string or existing non-anyhow error value.
210+
#[macro_export]
211+
macro_rules! range_not_satisfiable {
212+
($($arg:tt)*) => {{
213+
Error::from_err(anyhow::anyhow!($($arg)*), Some(StatusCode::RANGE_NOT_SATISFIABLE))
214+
}}
215+
}
216+
217+
/// Construct an `Error` with `StatusCode::EXPECTATION_FAILED` from a string or existing non-anyhow error value.
218+
#[macro_export]
219+
macro_rules! expectation_failed {
220+
($($arg:tt)*) => {{
221+
Error::from_err(anyhow::anyhow!($($arg)*), Some(StatusCode::EXPECTATION_FAILED))
222+
}}
223+
}
224+
225+
// 50x
226+
/// Construct an `Error` with `StatusCode::INTERNAL_SERVER_ERROR` from a string or existing non-anyhow error value.
227+
#[macro_export]
228+
macro_rules! internal_server_error {
229+
($($arg:tt)*) => {{
230+
Error::from_err(anyhow::anyhow!($($arg)*), Some(StatusCode::INTERNAL_SERVER_ERROR))
231+
}}
232+
}
233+
234+
/// Construct an `Error` with `StatusCode::NOT_IMPLEMENTED` from a string or existing non-anyhow error value.
235+
#[macro_export]
236+
macro_rules! not_implemented {
237+
($($arg:tt)*) => {{
238+
Error::from_err(anyhow::anyhow!($($arg)*), Some(StatusCode::NOT_IMPLEMENTED))
239+
}}
240+
}
241+
242+
/// Construct an `Error` with `StatusCode::BAD_GATEWAY` from a string or existing non-anyhow error value.
243+
#[macro_export]
244+
macro_rules! bad_gateway {
245+
($($arg:tt)*) => {{
246+
Error::from_err(anyhow::anyhow!($($arg)*), Some(StatusCode::BAD_GATEWAY))
247+
}}
248+
}
249+
250+
/// Construct an `Error` with `StatusCode::SERVICE_UNAVAILABLE` from a string or existing non-anyhow error value.
251+
#[macro_export]
252+
macro_rules! service_unavailable {
253+
($($arg:tt)*) => {{
254+
Error::from_err(anyhow::anyhow!($($arg)*), Some(StatusCode::SERVICE_UNAVAILABLE))
255+
}}
256+
}
257+
258+
/// Construct an `Error` with `StatusCode::GATEWAY_TIMEOUT` from a string or existing non-anyhow error value.
259+
#[macro_export]
260+
macro_rules! gateway_timeout {
261+
($($arg:tt)*) => {{
262+
Error::from_err(anyhow::anyhow!($($arg)*), Some(StatusCode::GATEWAY_TIMEOUT))
263+
}}
264+
}
265+
266+
/// Construct an `Error` with `StatusCode::HTTP_VERSION_NOT_SUPPORTED` from a string or existing non-anyhow error value.
267+
#[macro_export]
268+
macro_rules! http_version_not_supported {
269+
($($arg:tt)*) => {{
270+
Error::from_err(anyhow::anyhow!($($arg)*), Some(StatusCode::HTTP_VERSION_NOT_SUPPORTED))
271+
}}
272+
}
273+
274+
/// Construct an `Error` with `StatusCode::VARIANT_ALSO_NEGOTIATES` from a string or existing non-anyhow error value.
275+
#[macro_export]
276+
macro_rules! variant_also_negotiates {
277+
($($arg:tt)*) => {{
278+
Error::from_err(anyhow::anyhow!($($arg)*), Some(StatusCode::VARIANT_ALSO_NEGOTIATES))
279+
}}
280+
}
281+
282+
/// Construct an `Error` with `StatusCode::INSUFFICIENT_STORAGE` from a string or existing non-anyhow error value.
283+
#[macro_export]
284+
macro_rules! insufficient_storage {
285+
($($arg:tt)*) => {{
286+
Error::from_err(anyhow::anyhow!($($arg)*), Some(StatusCode::INSUFFICIENT_STORAGE))
287+
}}
288+
}
289+
290+
/// Construct an `Error` with `StatusCode::LOOP_DETECTED` from a string or existing non-anyhow error value.
291+
#[macro_export]
292+
macro_rules! loop_detected {
293+
($($arg:tt)*) => {{
294+
Error::from_err(anyhow::anyhow!($($arg)*), Some(StatusCode::LOOP_DETECTED))
75295
}}
76296
}

0 commit comments

Comments
 (0)