anyhow

Macro bail

source
macro_rules! bail {
    ($msg:literal $(,)?) => { ... };
    ($err:expr $(,)?) => { ... };
    ($fmt:expr, $($arg:tt)*) => { ... };
}
Expand description

Return early with an error.

This macro is equivalent to return Err(anyhow!($args...)).

The surrounding function’s or closure’s return value is required to be Result<_,anyhow::Error>.

§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);
}