@@ -176,3 +176,42 @@ impl<E: std::error::Error + 'static> From<E> for GenericErrorWithCause {
176
176
}
177
177
}
178
178
}
179
+
180
+ /// Creates a new `GenericErrorWithCause` efficiently. This allows you to explicitly return errors from anything that returns
181
+ /// `RenderFnResultWithCause`, which includes both an error and a statement of whether the server or the client is responsible. With
182
+ /// this macro, you can use any of the following syntaxes (substituting `"error!"` for any error that can be converted with `.into()`
183
+ /// into a `Box<dyn std::error::Error>`):
184
+ ///
185
+ /// - `blame_err!(client, "error!")` -- an error that's the client's fault, with the default HTTP status code (400, a generic client error)
186
+ /// - `blame_err!(server, "error!")` -- an error that's the server's fault, with the default HTTP status code (500, a generic server error)
187
+ /// - `blame_err!(client, 404, "error!")` -- an error that's the client's fault, with a custom HTTP status code (404 in this example)
188
+ /// - `blame_err!(server, 501, "error!")` -- an error that's the server's fault, with a custom HTTP status code (501 in this example)
189
+ ///
190
+ /// Note that this macro will automatically `return` the error it creates.
191
+ #[ macro_export]
192
+ macro_rules! blame_err {
193
+ ( client, $err: expr) => {
194
+ return :: std:: result:: Result :: Err ( :: perseus:: GenericErrorWithCause {
195
+ error: $err. into( ) ,
196
+ cause: $crate:: ErrorCause :: Client ( :: std:: option:: Option :: None ) ,
197
+ } )
198
+ } ;
199
+ ( client, $code: literal, $err: expr) => {
200
+ return :: std:: result:: Result :: Err ( :: perseus:: GenericErrorWithCause {
201
+ error: $err. into( ) ,
202
+ cause: $crate:: ErrorCause :: Client ( :: std:: option:: Option :: Some ( $code) ) ,
203
+ } )
204
+ } ;
205
+ ( server, $err: expr) => {
206
+ return :: std:: result:: Result :: Err ( :: perseus:: GenericErrorWithCause {
207
+ error: $err. into( ) ,
208
+ cause: $crate:: ErrorCause :: Server ( :: std:: option:: Option :: None ) ,
209
+ } )
210
+ } ;
211
+ ( server, $code: literal, $err: expr) => {
212
+ return :: std:: result:: Result :: Err ( :: perseus:: GenericErrorWithCause {
213
+ error: $err. into( ) ,
214
+ cause: $crate:: ErrorCause :: Server ( :: std:: option:: Option :: Some ( $code) ) ,
215
+ } )
216
+ } ;
217
+ }
0 commit comments