[][src]Macro molt::molt_throw

macro_rules! molt_throw {
    ($code:expr, $msg:expr) => { ... };
    ($code:expr, $($arg:tt)*) => { ... };
}

Returns an Error MoltResult with a specific error code. The error message is formatted as with format!().

The macro requires two or more arguments. The first argument is always the error code. If called with two arguments, the second is the error message. If called with more than two arguments, the second is a format!() format string and the remainder are the values to format.

This macro wraps the Exception::molt_err2 method.

Examples

use molt::*;

// Throw a simple error
fn throw1() -> MoltResult {
    // ...
    molt_throw!("MYCODE", "error message")
}

let result = throw1();
assert!(result.is_err());

let exception = result.err().unwrap();
assert!(exception.is_error());
assert_eq!(exception.value(), "error message".into());
assert_eq!(exception.error_code(), "MYCODE".into());

// Return a formatted error
fn throw2() -> MoltResult {
   // ...
   molt_throw!("MYCODE", "invalid value: {}", 17)
}

let result = throw2();
assert!(result.is_err());

let exception = result.err().unwrap();
assert!(exception.is_error());
assert_eq!(exception.value(), "invalid value: 17".into());
assert_eq!(exception.error_code(), "MYCODE".into());