error messages don't compose well with anyhow #533
Labels
No labels
breaking
bug
dependencies
good first issue
help wanted
unstable
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
gh-uuid-rs/uuid#533
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Describe the bug
I've been using @dtolnay's anyhow crate as a great way to annotate errors with context about was happening when an error was generated. I found that with
uuid, though, it generates duplicate messages.To Reproduce
Steps to reproduce the behavior:
cargo runHere's the relevant code:
Expected behavior
I expected this to print:
but it actually prints:
Screenshots
The rest of the program shows exactly what the error chain looks like. Here's the output:
Specifications (please complete the following information):
This is with
uuid0.8.2,anyhow1.0.44, rust 1.52.0, on MacOS 11.6 (Big Sur). I haven't enabled any features. (You can see the complete details in the Cargo.toml and Cargo.lock in the repo I linked to.)Additional context
anyhowuses the alternate Display format to print the whole cause chain as a single message. I like this a lot because it prints errors the way most standard Unix tools do.I believe the way it works is it follows the cause chain using std::error::Error::source() and prints out the message associated with each one. This seems pretty reasonable.
I think the problem is that the uuid crate uses a few levels of Error (i.e., a cause chain), and
Displayon the higher level impl delegates to the next level. So if one walks the cause chain and prints each one out, you get the duplicated message. This structure doesn't really seem right, though I'm not sure the best fix because I'm not sure why the indirection.Thanks for the report @davepacheco! It looks like we need to take a fresh look at our error handling story. If this is something you’re interested in exploring please feel free to dig into it!
Alrighty, I’ve reworked the internal shape of
uuid::Errorso it now reports its inner context throughDisplayinstead ofsource. That should make it work better withanyhow.Thanks for doing that! It looks great. If I switch the test repo to use commit
2925da3372(current tip of main), the output looks like this:which is perfect. Thanks!
Any idea when this will wind up in a release on crates.io?