Skip to main content

bail

Macro bail 

Source
macro_rules! bail {
    ( $($args:tt)* ) => { ... };
}
Expand description

Early exit from the current function with an error.

This helper is equivalent to return Err(anyhow!(...)).

See the docs for the anyhow! macro for details on the kinds of errors that can be constructed.

Like anyhow::bail! but for wasmtime::Error.

ยงExample

use wasmtime::{bail, Result};

fn error_on_none(option: Option<u32>) -> Result<u32> {
    match option {
        None => bail!("`error_on_none` got `None`!"),
        Some(x) => Ok(x),
    }
}

let x = error_on_none(Some(42)).unwrap();
assert_eq!(x, 42);

let error = error_on_none(None).unwrap_err();
assert_eq!(
    error.to_string(),
    "`error_on_none` got `None`!",
);