Skip to main content

bail

Macro bail 

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

Return an Error of a kind with a formatted message from the current function.

bail!(kind, fmt, args...) formats the message as format! does and records the source location in the backtrace.

§Arguments

  • kind - The kind of the error, such as VALUE_ERROR.
  • fmt - The format string of the message.
  • args - The format arguments.

§Returns

Returns Err of the error from the enclosing function.

§Examples

use tvm_ffi::{bail, Result, INDEX_ERROR};

fn get(items: &[i64], index: usize) -> Result<i64> {
    if index >= items.len() {
        bail!(INDEX_ERROR, "index {} out of range for length {}", index, items.len());
    }
    Ok(items[index])
}

assert_eq!(get(&[1, 2], 1)?, 2);
assert_eq!(get(&[1, 2], 5).unwrap_err().message(), "index 5 out of range for length 2");