macro_rules! ensure {
($condition:expr, $ctx:expr, $($error:tt)+) => { ... };
}Expand description
Return a JSON:API error unless a condition holds.
The guard clause a handler’s preconditions are written as — the
counterpart of assert! for something that is the client’s fault rather
than a bug. Everything after the condition is passed to
invalid_attribute!, so the member at fault
is named where there is one.
fn rename(name: &str, ctx: &Context) -> Result<(), JsonApiError> {
ash_jsonapi::ensure!(!name.is_empty(), ctx, "name must not be empty", "name");
ash_jsonapi::ensure!(name.len() <= 80, ctx, "name must be 80 characters or fewer", "name");
Ok(())
}Use ensure_code! for a guard that is not a 422.