bail

Macro bail 

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

Returns early with an Error.

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

ยงExamples

use anyhow_tracing::{bail, Result};

fn example() -> Result<()> {
    bail!("Something went wrong");
}

fn example_with_fields() -> Result<()> {
    bail!(user_id = %"123", "User not found");
}

fn example_with_implicit_fields() -> Result<()> {
    bail!(user_id = "123", "User not found");
}

fn example_with_semicolon() -> Result<()> {
    let id = "123";
    bail!(user_id = id; "User not found");
}

// You can also use a variable directly as a field (variable name becomes field name)
fn example_with_positional() -> Result<()> {
    let id = "123";
    bail!(id, "User not found");
}