-
Notifications
You must be signed in to change notification settings - Fork 111
Add a method to format chained errors. #113
Conversation
if let Some(backtrace) = e.backtrace() { | ||
writeln!(stderr, "{:?}", backtrace).expect(errmsg); | ||
} | ||
write!(stderr, "{}", $crate::ChainedError::display(e)).expect(errmsg); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can inline errmsg
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
@@ -444,6 +455,27 @@ pub trait ChainedError: error::Error + Send + 'static { | |||
where Self: Sized; | |||
} | |||
|
|||
/// A struct which formats an error for output. | |||
pub struct Display<'a, T: 'a>(&'a T); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe also impl Debug
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it produce the same output as Display
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nevermind, derived it.
/// | ||
/// The full cause chain and backtrace, if present, will be printed. | ||
fn display<'a>(&'a self) -> Display<'a, Self> | ||
where Self: Sized |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you sure you need this part?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep:
error[E0277]: the trait bound `Self: std::marker::Sized` is not satisfied
--> src/lib.rs:440:5
|
440 | fn display<'a>(&'a self) -> Display<'a, Self> {
| ^ the trait `std::marker::Sized` is not implemented for `Self`
|
= help: consider adding a `where Self: std::marker::Sized` bound
= note: required by `Display`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, nevermind, I'm dumb.
Nice! Could you also update the changelog? |
The main function isn't the only place you might want to print the full error message, for example, to log error messages produced by a handler in a web server.
Updated |
Cool! |
Any idea when this'll land in a release? |
Done! |
Thanks! |
The main function isn't the only place you might want to print the full
error message, for example, to log error messages produced by a handler
in a web server.