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
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 error chains in
Cause
Imagine module A imports module B and both use `pkg/errors`. A uses `errors.Cause` to inspect wrapped errors returned from B. As-is, B cannot migrate from `errors.Wrap` to `fmt.Errorf("%w", err)` because that would break `errors.Cause` calls in A. With this change merged, `errors.Cause` becomes forwards-compatible with Go 1.13 error chains. Module B will be free to switch to `fmt.Errorf("%w", err)` and that will not break module A (so long as the top-level project pulls in the newer version of `pkg/errors`).
- Loading branch information
Showing
4 changed files
with
77 additions
and
27 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,29 @@ | ||
// +build !go1.13 | ||
|
||
package errors | ||
|
||
// Cause returns the underlying cause of the error, if possible. | ||
// An error value has a cause if it implements the following | ||
// interface: | ||
// | ||
// type causer interface { | ||
// Cause() error | ||
// } | ||
// | ||
// If the error does not implement Cause, the original error will | ||
// be returned. If the error is nil, nil will be returned without further | ||
// investigation. | ||
func Cause(err error) error { | ||
type causer interface { | ||
Cause() error | ||
} | ||
|
||
for err != nil { | ||
cause, ok := err.(causer) | ||
if !ok { | ||
break | ||
} | ||
err = cause.Cause() | ||
} | ||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// +build go1.13 | ||
|
||
package errors | ||
|
||
import "errors" | ||
|
||
// Cause returns the underlying cause of the error, if possible. | ||
// An error value has a cause if it implements the following | ||
// interface: | ||
// | ||
// type causer interface { | ||
// Cause() error | ||
// } | ||
// | ||
// If the error does not implement the Cause interface, the Cause | ||
// function will fallback to the standard library's errors.Unwrap | ||
// that was added in Go 1.13. This makes Cause forwards-compatible | ||
// with Go 1.13 error chains. | ||
// | ||
// If the error is nil, nil will be returned without further | ||
// investigation. | ||
func Cause(err error) error { | ||
type causer interface { | ||
Cause() error | ||
} | ||
|
||
for err != nil { | ||
if cause, ok := err.(causer); ok { | ||
err = cause.Cause() | ||
} else if unwrapped := errors.Unwrap(err); unwrapped != nil { | ||
err = unwrapped | ||
} else { | ||
break | ||
} | ||
} | ||
return 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
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