tailhook/quick-error

File name and line number for error?

martinellison opened this issue · 2 comments

Is there any way of getting file+line number into the error message?

Sorry for the late reply. You're probably going to use either:

enum Error {
  SomeError { file: &'static str, line: usize }
}
return Err(SomeError { file: file!(), line: line!() })

Or the backtrace:

use backtrace::Backtrace;
enum Error {
  SomeError { backtrace: Backtrace }
}
return Err(SomeError { backtrace: Backtrace::new() })

We don't have any built-in helpers to make that easier. You can make a function for creating an error with a backtrace. Abstracting the file and line variant can only be via a macro though.

And surely, you can put that into a format string, if that's what you're asking:

quick_error! {
    #[derive(Debug)]
    pub enum Error {
        MyError(file: &'static str, line: usize) {
            display("MyError at {}:{}", file, line)
        }
    }
}