Macro trackable::track_err [] [src]

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

Error tracking macro for the types which have map_err method.

Unlink track_try! macro, This does not require that the expr is evaluated to a std::result::Result value. And it will not return from the current function, even if the value of expr is erroneous.

use trackable::error::Failure;

enum MyResult<E> {
    Ok(usize),
    Err(E),
}
impl<E> MyResult<E> {
    fn map_err<F, T>(self, f: F) -> MyResult<T> where F: FnOnce(E) -> T {
        match self {
            MyResult::Err(e) => MyResult::Err(f(e)),
            MyResult::Ok(v) => MyResult::Ok(v),
        }
    }
    fn err(self) -> Option<E> {
        if let MyResult::Err(e) = self { Some(e) } else { None }
    }
}

let result = MyResult::Err("something wrong");
let result: MyResult<Failure> = track_err!(result);
let result: MyResult<Failure> = track_err!(result, "Hello World!");

let e = result.err().unwrap();
assert_eq!(format!("\n{}", e), r#"
Failed (cause; something wrong)
HISTORY:
  [0] at <anon>:24
  [1] at <anon>:25; Hello World!
"#);