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

Add a method to format chained errors. #113

Merged
merged 5 commits into from
Jan 9, 2017

Conversation

sfackler
Copy link
Contributor

@sfackler sfackler commented Jan 9, 2017

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.

if let Some(backtrace) = e.backtrace() {
writeln!(stderr, "{:?}", backtrace).expect(errmsg);
}
write!(stderr, "{}", $crate::ChainedError::display(e)).expect(errmsg);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can inline errmsg.

Copy link
Contributor Author

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also impl Debug.

Copy link
Contributor Author

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?

Copy link
Contributor Author

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
Copy link
Contributor

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?

Copy link
Contributor Author

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`

Copy link
Contributor Author

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.

@Yamakaky
Copy link
Contributor

Yamakaky commented Jan 9, 2017

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.
@sfackler
Copy link
Contributor Author

sfackler commented Jan 9, 2017

Updated

@Yamakaky
Copy link
Contributor

Yamakaky commented Jan 9, 2017

Cool!

@Yamakaky Yamakaky merged commit ede14cc into rust-lang-deprecated:master Jan 9, 2017
@sfackler sfackler deleted the display branch January 18, 2017 11:53
@sfackler
Copy link
Contributor Author

Any idea when this'll land in a release?

@Yamakaky
Copy link
Contributor

Done!

@sfackler
Copy link
Contributor Author

Thanks!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants