macro_rules! bail { ($($key:ident = $value:expr,)* $fmt:literal $($arg:tt)*) => { ... }; ($err:expr $(,)?) => { ... }; }
Expand description
Return early with an error.
This macro is equivalent to return Err(From::from($err))
.
ยงExample
if !has_permission(user, resource) {
bail!("permission denied for accessing {resource}");
}
#[derive(Error, Debug)]
enum ScienceError {
#[error("recursion limit exceeded")]
RecursionLimitExceeded,
...
}
if depth > MAX_DEPTH {
bail!(ScienceError::RecursionLimitExceeded);
}
use miette::{bail, Result, Severity};
fn divide(x: f64, y: f64) -> Result<f64> {
if y.abs() < 1e-3 {
bail!(
severity = Severity::Warning,
"dividing by value ({y}) close to 0"
);
}
Ok(x / y)
}