Skip to content
This repository has been archived by the owner on Aug 16, 2021. It is now read-only.

support at method using for locate error #332

Open
s97712 opened this issue Oct 22, 2019 · 0 comments
Open

support at method using for locate error #332

s97712 opened this issue Oct 22, 2019 · 0 comments

Comments

@s97712
Copy link

s97712 commented Oct 22, 2019

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

@s97712 s97712 changed the title support Position using for locate error support at method using for locate error Oct 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant