http_error/
result_ext.rs

1use std::{error, fmt};
2
3use http::StatusCode;
4
5use crate::HttpError;
6
7#[derive(Debug)]
8pub struct StatusError<E> {
9    status: StatusCode,
10    inner: E,
11}
12
13macro_rules! trait_fn {
14    ($name:ident) => {
15        fn $name(self) -> Result<T, StatusError<E>>;
16    };
17}
18macro_rules! impl_fn {
19    ($name:ident, $status:ident) => {
20        fn $name(self) -> Result<T, StatusError<E>> {
21            self.map_err(|inner| StatusError {
22                status: StatusCode::$status,
23                inner,
24            })
25        }
26    };
27}
28
29pub trait ResultExt<T, E> {
30    // 400 Bad Request
31    trait_fn!(bad_request);
32    // 401 Unauthorized
33    trait_fn!(unauthorized);
34    // 402 Payment Required
35    trait_fn!(payment_required);
36    // 403 Forbidden
37    trait_fn!(forbidden);
38    // 404 Not Found
39    trait_fn!(not_found);
40    // 405 Method Not Allowed
41    trait_fn!(method_not_allowed);
42    // 406 Not Acceptable
43    trait_fn!(not_acceptable);
44    // 407 Proxy Authentication Required
45    trait_fn!(proxy_authentication_required);
46    // 408 Request Timeout
47    trait_fn!(request_timeout);
48    // 409 Conflict
49    trait_fn!(conflict);
50    // 410 Gone
51    trait_fn!(gone);
52    // 411 Length Required
53    trait_fn!(length_required);
54    // 412 Precondition Failed
55    trait_fn!(precondition_failed);
56    // 413 Payload Too Large
57    trait_fn!(payload_too_large);
58    // 414 URI Too Long
59    trait_fn!(uri_too_long);
60    // 415 Unsupported Media Type
61    trait_fn!(unsupported_media_type);
62    // 416 Range Not Satisfiable
63    trait_fn!(range_not_satisfiable);
64    // 417 Expectation Failed
65    trait_fn!(expectation_failed);
66    // 418 I'm a teapot
67    trait_fn!(im_a_teapot);
68
69    // 421 Misdirected Request
70    trait_fn!(misdirected_request);
71    // 422 Unprocessable Entity
72    trait_fn!(unprocessable_entity);
73    // 423 Locked
74    trait_fn!(locked);
75    // 424 Failed Dependency
76    trait_fn!(failed_dependency);
77
78    // 426 Upgrade Required
79    trait_fn!(upgrade_required);
80
81    // 428 Precondition Required
82    trait_fn!(precondition_required);
83    // 429 Too Many Requests
84    trait_fn!(too_many_requests);
85
86    // 431 Request Header Fields Too Large
87    trait_fn!(request_header_fields_too_large);
88
89    // 451 Unavailable For Legal Reasons
90    trait_fn!(unavailable_for_legal_reasons);
91
92    // 500 Internal Server Error
93    trait_fn!(internal_server_error);
94    // 501 Not Implemented
95    trait_fn!(not_implemented);
96    // 502 Bad Gateway
97    trait_fn!(bad_gateway);
98    // 503 Service Unavailable
99    trait_fn!(service_unavailable);
100    // 504 Gateway Timeout
101    trait_fn!(gateway_timeout);
102    // 505 HTTP Version Not Supported
103    trait_fn!(http_version_not_supported);
104    // 506 Variant Also Negotiates
105    trait_fn!(variant_also_negotiates);
106    // 507 Insufficient Storage
107    trait_fn!(insufficient_storage);
108    // 508 Loop Detected
109    trait_fn!(loop_detected);
110
111    // 510 Not Extended
112    trait_fn!(not_extended);
113    // 511 Network Authentication Required
114    trait_fn!(network_authentication_required);
115}
116impl<T, E> ResultExt<T, E> for Result<T, E> {
117    impl_fn!(bad_request, BAD_REQUEST);
118    impl_fn!(unauthorized, UNAUTHORIZED);
119    impl_fn!(payment_required, PAYMENT_REQUIRED);
120    impl_fn!(forbidden, FORBIDDEN);
121    impl_fn!(not_found, NOT_FOUND);
122    impl_fn!(method_not_allowed, METHOD_NOT_ALLOWED);
123    impl_fn!(not_acceptable, NOT_ACCEPTABLE);
124    impl_fn!(proxy_authentication_required, PROXY_AUTHENTICATION_REQUIRED);
125    impl_fn!(request_timeout, REQUEST_TIMEOUT);
126    impl_fn!(conflict, CONFLICT);
127    impl_fn!(gone, GONE);
128    impl_fn!(length_required, LENGTH_REQUIRED);
129    impl_fn!(precondition_failed, PRECONDITION_FAILED);
130    impl_fn!(payload_too_large, PAYLOAD_TOO_LARGE);
131    impl_fn!(uri_too_long, URI_TOO_LONG);
132    impl_fn!(unsupported_media_type, UNSUPPORTED_MEDIA_TYPE);
133    impl_fn!(range_not_satisfiable, RANGE_NOT_SATISFIABLE);
134    impl_fn!(expectation_failed, EXPECTATION_FAILED);
135    impl_fn!(im_a_teapot, IM_A_TEAPOT);
136
137    impl_fn!(misdirected_request, MISDIRECTED_REQUEST);
138    impl_fn!(unprocessable_entity, UNPROCESSABLE_ENTITY);
139    impl_fn!(locked, LOCKED);
140    impl_fn!(failed_dependency, FAILED_DEPENDENCY);
141
142    impl_fn!(upgrade_required, UPGRADE_REQUIRED);
143
144    impl_fn!(precondition_required, PRECONDITION_REQUIRED);
145    impl_fn!(too_many_requests, TOO_MANY_REQUESTS);
146
147    impl_fn!(
148        request_header_fields_too_large,
149        REQUEST_HEADER_FIELDS_TOO_LARGE
150    );
151
152    impl_fn!(unavailable_for_legal_reasons, UNAVAILABLE_FOR_LEGAL_REASONS);
153
154    impl_fn!(internal_server_error, INTERNAL_SERVER_ERROR);
155    impl_fn!(not_implemented, NOT_IMPLEMENTED);
156    impl_fn!(bad_gateway, BAD_GATEWAY);
157    impl_fn!(service_unavailable, SERVICE_UNAVAILABLE);
158    impl_fn!(gateway_timeout, GATEWAY_TIMEOUT);
159    impl_fn!(http_version_not_supported, HTTP_VERSION_NOT_SUPPORTED);
160    impl_fn!(variant_also_negotiates, VARIANT_ALSO_NEGOTIATES);
161    impl_fn!(insufficient_storage, INSUFFICIENT_STORAGE);
162    impl_fn!(loop_detected, LOOP_DETECTED);
163
164    impl_fn!(not_extended, NOT_EXTENDED);
165    impl_fn!(
166        network_authentication_required,
167        NETWORK_AUTHENTICATION_REQUIRED
168    );
169}
170
171impl<E> error::Error for StatusError<E>
172where
173    E: error::Error + 'static,
174{
175    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
176        Some(&self.inner)
177    }
178}
179
180impl<E> fmt::Display for StatusError<E>
181where
182    E: fmt::Display,
183{
184    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
185        self.inner.fmt(f)
186    }
187}
188
189impl<E> HttpError for StatusError<E>
190where
191    E: error::Error + 'static,
192{
193    fn status_code(&self) -> StatusCode {
194        self.status
195    }
196}