rust-lang-deprecated / failure

Error management
https://rust-lang-nursery.github.io/failure/
Apache License 2.0
1.42k stars 140 forks source link

support `at` method using for locate error #332

Open s97712 opened 4 years ago

s97712 commented 4 years ago

Backtrace is too verbose and inefficient, we should provide another way to locate errors. By adding at method to Fail, We can write the code such as:

use position::{here, Position};
use failure::Fail;

#[derive(Fail, Debug)]
pub enum Bar {
  // `Position` using for record error location
  #[fail(display="foo")]
  Foo(Position)
}

fn cause_error() -> Bar {
  Bar::Foo(here!())
}

fn main() {
  let err = cause_error();

  let mut fail: &Fail = &err;

  while let Some(cause) = fail.cause() {
    let position = cause.at().map_or("...".to_owned(), |pos| pos.to_string());
    println!("\t{} at {}",cause, position);
    fail = cause;
  }

}

It will print the error location.

Related library

Position