use thiserror to remove error.rs boilerplate #376
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
In #296 (comment) we discussed using
thiserror
to remove some boilerplate in generatingError
andFrom
implementations, although we were able to usedisplaydoc
in that PR to implDisplay
and decided to table the issue pending some ergonomics improvements in the upstreamthiserror
crate.Solution
In dtolnay/thiserror#155 we were able to add the necessary internal trait implementation for
UnwindSafe
boxeddyn Error
s to enable using the declarative#[source]
attribute fromthiserror
without needing to introduce an annoyingCallbackErrorWrapper
to all callsites as #296 had originally attempted. This was quickly merged and released asthiserror
version1.0.30
.Result
As in #287 (comment) where
thiserror
was first proposed, there's a bit of a balancing act between pulling in an additional dependency + a potentially confusing DSL vs the ability to succinctly describe all of the ways that a variant of ourcrate::Error
can be used at its point of definition. I think that the#[from]
and#[source]
attributes introduced in this PR make it easier for a client to understand the meaning of an error and also clarify more readily for future developers of this codebase how to generate a meaningful, canonical error.