-
-
Notifications
You must be signed in to change notification settings - Fork 324
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
Don't include sources in their errors' display()
#515
Conversation
👍 on adding I'm not quite so sure about removing the source from the |
Not seeing anything about the alternate display mode in |
Oh, you're totally right, the |
My point still stands w.r.t. |
When trying to use `source()` in a chain, each `display()` currently includes the succeeding errors, causing a lot of duplication.
Including the source in
For
|
Now that we're getting more into opinions, my vote would go towards putting sources into both
|
Found rust-lang/project-error-handling#27 which is more relevant to us a library. And more up to date comment:
|
We should follow the official guidelines, but we don't have the necessary tools yet (they're planning to add I'm more concerned about not having focused errors (lightly discussed in #453, #453 (reply in thread)). |
I bumped into this very same issue recently - specifically when trying to publish Kubernetes' events in a controller.
By only returning the underlying error in fn error_chain_fmt(e: &impl std::error::Error) -> String {
let mut buffer = String::new();
writeln!(buffer, "{}\n", e).unwrap();
let mut current = e.source();
while let Some(cause) = current {
writeln!(buffer, "Caused by:\n\t{}", cause).unwrap();
current = cause.source();
}
buffer
} Adding |
It's not the prettiest, but you could do something along the lines of: let mut msg = err.to_string();
if let Some(source) = err.source() {
msg = msg.trim_end_matches(format!(": {}", source)).to_string();
}
You can, but the question is what people actually do. FWIW, tracing's console formatter only fixed this less than two weeks ago (and the JSON formatter still doesn't, over backwards compat concerns). |
That looks very brittle :/
I am not arguing with the broader underlying assumption - the Rust community is still, collectively, iterating on what are considered the best patterns for error handling. Tools in the space have some catch-up to do. |
Absolutely, please don't try to parse the That said, in this particular case, the only way that I can imagine we'd change this would be to remove the source entirely from the |
We'll do this as part of refining errors (#688). |
When trying to use
source()
in a chain, eachdisplay()
currently includes the succeeding errors, causing a lot of duplication.When one wants to easily display an error chain intercalated by ": ", they should format using the alternate format: format!("{:#}", err)
Also fixed extraneous quoting around paths in errors