[][src]Macro anyhow::bail

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

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