Skip to content

Commit 6ab178a

Browse files
committed
feat(templates): ✨ added blame_err! convenience macro
1 parent 7a3dd63 commit 6ab178a

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-7
lines changed

examples/showcase/src/templates/post.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use perseus::{
2-
ErrorCause, GenericErrorWithCause, RenderFnResult, RenderFnResultWithCause, Template,
3-
};
1+
use perseus::{blame_err, RenderFnResult, RenderFnResultWithCause, Template};
42
use serde::{Deserialize, Serialize};
53
use sycamore::prelude::{component, template, GenericNode, Template as SycamoreTemplate};
64

@@ -40,10 +38,11 @@ pub async fn get_static_props(
4038
) -> RenderFnResultWithCause<PostPageProps> {
4139
// This path is illegal, and can't be rendered
4240
if path == "post/tests" {
43-
return Err(GenericErrorWithCause {
44-
error: "illegal page".into(),
45-
cause: ErrorCause::Client(Some(404)),
46-
});
41+
// return Err(GenericErrorWithCause {
42+
// error: "illegal page".into(),
43+
// cause: ErrorCause::Client(Some(404)),
44+
// });
45+
blame_err!(client, 404, "illegal page");
4746
}
4847
// This is just an example
4948
let title = urlencoding::decode(&path).unwrap();

packages/perseus/src/errors.rs

+39
Original file line numberDiff line numberDiff line change
@@ -176,3 +176,42 @@ impl<E: std::error::Error + 'static> From<E> for GenericErrorWithCause {
176176
}
177177
}
178178
}
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

Comments
 (0)