This repository has been archived by the owner on Dec 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 699
feat: support std errors functions #213
Merged
Merged
Changes from 12 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
69e8329
feat: support std errors functions
Sherlock-Holo 5526281
style: delete useless comments
Sherlock-Holo 227a7b4
build: update makefile
Sherlock-Holo 9c503b9
build: fix makefile
Sherlock-Holo 34b42fb
chore: delete useless comments
Sherlock-Holo 057b7e2
Restore Makefile
Sherlock-Holo d9389f6
revert: revert some change
Sherlock-Holo c93d5ce
Merge remote-tracking branch 'pkg/master' into support-std-errors
Sherlock-Holo d0d34df
test: add more check for As unit test
Sherlock-Holo 487ff7b
revert: only support Is As Unwrap for >=go1.13
Sherlock-Holo e473c5d
feat(Unwrap): allow <go1.13 can use Unwrap
Sherlock-Holo bd678d4
test: add go1.13 errors compatibility check
Sherlock-Holo 613b81c
refactor(Unwrap): don't allow <go1.13 use Unwrap
Sherlock-Holo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// +build go1.13 | ||
|
||
package errors | ||
|
||
import ( | ||
stderrors "errors" | ||
) | ||
|
||
// Is reports whether any error in err's chain matches target. | ||
// | ||
// The chain consists of err itself followed by the sequence of errors obtained by | ||
// repeatedly calling Unwrap. | ||
// | ||
// An error is considered to match a target if it is equal to that target or if | ||
// it implements a method Is(error) bool such that Is(target) returns true. | ||
func Is(err, target error) bool { return stderrors.Is(err, target) } | ||
|
||
// As finds the first error in err's chain that matches target, and if so, sets | ||
// target to that error value and returns true. | ||
// | ||
// The chain consists of err itself followed by the sequence of errors obtained by | ||
// repeatedly calling Unwrap. | ||
// | ||
// An error matches target if the error's concrete value is assignable to the value | ||
// pointed to by target, or if the error has a method As(interface{}) bool such that | ||
// As(target) returns true. In the latter case, the As method is responsible for | ||
// setting target. | ||
// | ||
// As will panic if target is not a non-nil pointer to either a type that implements | ||
// error, or to any interface type. As returns false if err is nil. | ||
func As(err error, target interface{}) bool { return stderrors.As(err, target) } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why reimplement this function rather than wrapping it? Someone using Go 1.12 or older will not be using the new style of errors. This creates a risk of incompatibility if Go 1.14 subtly changes its
Unwrap
implementation.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aperezg emm.... this is a problem if go1.14 changes
Unwrap
implment, so... we should keepUnwrap
>= go1.13?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But why is the problem to maintain the Unwrap function like today? I understand that the Unwrap is an interface on a std library right? Sorry, I try to understand what is the point to change the implementation a wrapped with the standard library. I doubt that they implement a breaking change on that, no?
https://github.com/pkg/errors/blob/master/errors.go#L163
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
go1.14 cannot break the semantics of
Unwrap
without violating the go1 compat guarantee.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This functionality should likely be folded into
Cause()
andUnwrap()
here just callsCause()
.c.f. #215
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as that says, 1.14 errors won't change implementation, so we could make unwrap continue in normal package @aperezg
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That
Unwrap
only works for specific permutations of wrapping with the standard library andcauser
s.In the wild, we should expect any possible permutation of wrapping, including multiple times being wrapped in an alternating manner.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unwrap
should only unwrap error one times, no matter the inner error implement unwrap interface or not, like stdUnwrap
do.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But this code would fail to fully unwrap:
Unwrap(fmt.Errorf("wrap1: %w", myPrivateCauserOnlyError(fmt.Errorf("wrap 3: %w", err), "wrap2:")))
where I would get thefmt.Errorf("wrap3: %w", err)
error not the originalerr
, which would not be the expected behavior.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤦♀ the standard library
Unwrap
does not continuously unwrap a function, until it reaches the end result, unlike howCause
in this package works.