-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Go 1.13 errors.As/Is/Unwrap functionality
The primary mechanism that enables this functionality is making `Unwrap` on the top-level Error return a new "chain" structure that uses state to keep track of the current error. The chain implements errors.Is/As so that it compares to that current underlying error. And it implements Unwrap to move on to the next error. A well-formed program using errors.Is/As/Unwrap exclusively will behave correctly with go-multierror in this case without dropping any errors. Direct comparisons such as `Unwrap() == myErr` will not work because we wrap in a chain. The user has to do `errors.Is(err, myErr)` which is the right thing to do anyways. When Unwrap is called on a top-level *Error, we create a shallow copy of the errors so that you can continue using *Error (modifying it, potentially in-place). There is a slight cost to this but it felt weird that calling Unwrap would share the same underlying data and cause potential data races. I think this is unlikely, but its also very unlikely the performance cost of the shallow copy of errors will matter either. Thanks to @felixge on Twitter for pushing me to do this, showing some examples on how it could be done, and providing feedback to this PR.
- Loading branch information
Showing
2 changed files
with
172 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters