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 700
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support std errors functions (#213)
* feat: support std errors functions add function `Is`, `As` and `Unwrap`, like std errors, so that we can continue to use pkg/errors with go1.13 compatibility Signed-off-by: Sherlock Holo <sherlockya@gmail.com> * style: delete useless comments Signed-off-by: Sherlock Holo <sherlockya@gmail.com> * build: update makefile update makefile to download dependencies before test anything Signed-off-by: Sherlock Holo <sherlockya@gmail.com> * build: fix makefile Signed-off-by: Sherlock Holo <sherlockya@gmail.com> * chore: delete useless comments Signed-off-by: Sherlock Holo <sherlockya@gmail.com> * Restore Makefile * revert: revert some change some change are doing by PR #206 and #212 , so I don't need to do it Signed-off-by: Sherlock Holo <sherlockya@gmail.com> * test: add more check for As unit test Signed-off-by: Sherlock Holo <sherlockya@gmail.com> * revert: only support Is As Unwrap for >=go1.13 Signed-off-by: Sherlock Holo <sherlockya@gmail.com> * feat(Unwrap): allow <go1.13 can use Unwrap `Unwrap` just use type assert, it doesn't need go1.13 actually Signed-off-by: Sherlock Holo <sherlockya@gmail.com> * test: add go1.13 errors compatibility check Signed-off-by: Sherlock Holo <sherlockya@gmail.com> * refactor(Unwrap): don't allow <go1.13 use Unwrap If we implement Unwrap ourselves, may create a risk of incompatibility if Go 1.14 subtly changes its `Unwrap` implementation. <go1.13 users doesn't have `Is` or `As`, if they want, they will use xerrors and it also provides `Unwrap` Signed-off-by: Sherlock Holo <sherlockya@gmail.com>
- Loading branch information
1 parent
7f95ac1
commit 6d954f5
Showing
2 changed files
with
203 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// +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) } | ||
|
||
// Unwrap returns the result of calling the Unwrap method on err, if err's | ||
// type contains an Unwrap method returning error. | ||
// Otherwise, Unwrap returns nil. | ||
func Unwrap(err error) error { | ||
return stderrors.Unwrap(err) | ||
} |
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