-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
hyper::Error
should either print source
in display xor return a Some
from Error::cause()
, not both
#2732
Comments
hyper::Error
should either print cause in display xor return a Some
from Error::cause()
, not bothhyper::Error
should either print source
in display xor return a Some
from Error::cause()
, not both
See also #2542 |
I should probably write up some thoughts as to why this is currently the way it is. Wrapping an error (so that there's an inner source) frequently provides more context that is useful to users. Without showing the source, the extra context can lose some of its meaning. For example, seeing just "error trying to connect" is also too vague, just as "Connection refused" can be. Most users of hyper tend to just log the I recognize that makes it more verbose for people using utilities to crawl the source chain. I currently chose to cater to the more common case. I'm also aware that the "error working group" currently recommends against doing what hyper does. But I don't agree, since it means hurting the common case. And since I believe most users will generally just default to logging |
@seanmonstar What do you think of making |
I think I also considered adding a method a la The second option feels nicer to me but either should work. |
To others who might find this. I used the new struct DedupHyperErrorSources {
error: hyper::Error,
}
impl DedupHyperErrorSources {
fn new(error: hyper::Error) -> Self {
Self { error }
}
}
impl fmt::Debug for DedupHyperErrorSources {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.error.fmt(f)
}
}
impl fmt::Display for DedupHyperErrorSources {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.error.message())
}
}
impl std::error::Error for DedupHyperErrorSources {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
if self.error.is_connect() {
// hyper's internal `ConnectError` also includes the source in its `Display` impl but
// otherwise doesn't add anything valuable to the source chain. So we can just skip it.
self.error.source()?.source()
} else {
self.error.source()
}
}
} |
Version
f1b89c1
Platform
Any
Description
Today the
std::fmt::Display
impl forhyper::Error
has the following snippet:hyper/src/error.rs
Lines 500 to 501 in f1b89c1
and the
std::error::Error::source
impl forhyper::Error
reads as such:hyper/src/error.rs
Lines 510 to 513 in f1b89c1
This is somewhat problematic for code that properly prints out the error causation chain. e.g. I have seen an error like this:
this is pretty unreadable. Now one might argue that
hyper::Error::description()
serves this use-case, and while that's true, it seems pretty awkward to pull in hyper into the code that's implements error presentation layer just to downcast and check forhyper
.The text was updated successfully, but these errors were encountered: