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

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][crate::Error]>.

Example

if !has_permission(user, resource) {
bail!("permission denied for accessing {}", resource);
}
1
2
3
4
5
6
7
8
9
10
#[derive(Error, Debug)]
enum ScienceError {
#[error("recursion limit exceeded")]
RecursionLimitExceeded,
...
}
if depth > MAX_DEPTH {
bail!(ScienceError::RecursionLimitExceeded);
}