Reputation: 375
I'm working with Rust and Rocket. At some point I can create a catcher to capture errors with:
#[catch(403)]
pub fn default_catcher() -> String {
return String::from("something interesting");
}
As you can imagine the macro #[catch(403)]
is capturing errors 403
.
Would it be possible to pass a constant to the macro? Something like:
const STATUS_CUSTOM_AUTHENTICATION_ERROR: u16 = 403;
#[catch(`${STATUS_CUSTOM_AUTHENTICATION_ERROR}`)]
...
Upvotes: 0
Views: 99
Reputation: 71485
Not with the macro.
If you want, you can write the boilerplate manually (please don't). The name of the static
is important (it's how Rocket finds it):
// This is your function.
pub fn default_catcher() -> String {
String::from("something interesting")
}
pub fn rocket_catch_fn_default_catcher(req: &rocket::Request) -> rocket::Result<'_> {
let response = rocket::Responder::respond_to(default_catcher(), req)?;
rocket::Response::build()
.status(rocket::http::Status::new(
STATUS_CUSTOM_AUTHENTICATION_ERROR,
"some reason (e.g. `Forbidden` for 403)",
))
.merge(response)
.ok()
}
#[allow(non_upper_case_globals)]
pub static static_rocket_catch_info_for_default_catcher: rocket::StaticCatchInfo =
rocket::StaticCatchInfo {
code: 403u16,
handler: rocket_catch_fn_default_catcher,
};
Keep in mind that these are implementation details and can change at any moment.
Upvotes: 1