Skip to main content

format_err

Macro format_err 

Source
macro_rules! format_err {
    ($error_kind:expr, $fmt:expr $(, $args:expr)* $(,)?) => { ... };
}
Expand description

Make an Error of a kind with a formatted message, recording the source location in the backtrace, as bail! does without returning.

use tvm_ffi::{format_err, Result, KEY_ERROR};

fn lookup(names: &[&str], name: &str) -> Result<usize> {
    names.iter().position(|n| *n == name).ok_or_else(|| format_err!(KEY_ERROR, "no {name}"))
}

assert_eq!(lookup(&["a"], "b").unwrap_err().to_string(), "KeyError: no b");