From 614d223910a179a466c1767a985424175c39b465 Mon Sep 17 00:00:00 2001 From: Adrian Perez Date: Tue, 14 Jan 2020 20:47:44 +0100 Subject: [PATCH] Revert "Support Go 1.13 error chains in `Cause` (#215)" (#220) This reverts commit 49f8f617296114c890ae0b7ac18c5953d2b1ca0f. --- cause.go | 29 ----------------------------- errors.go | 26 ++++++++++++++++++++++++++ go113.go | 33 --------------------------------- go113_test.go | 24 +----------------------- 4 files changed, 27 insertions(+), 85 deletions(-) delete mode 100644 cause.go diff --git a/cause.go b/cause.go deleted file mode 100644 index 566f88b..0000000 --- a/cause.go +++ /dev/null @@ -1,29 +0,0 @@ -// +build !go1.13 - -package errors - -// Cause recursively unwraps an error chain and 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 -} diff --git a/errors.go b/errors.go index a9840ec..161aea2 100644 --- a/errors.go +++ b/errors.go @@ -260,3 +260,29 @@ func (w *withMessage) Format(s fmt.State, verb rune) { io.WriteString(s, w.Error()) } } + +// 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 +} diff --git a/go113.go b/go113.go index ed0dc7a..be0d10d 100644 --- a/go113.go +++ b/go113.go @@ -36,36 +36,3 @@ func As(err error, target interface{}) bool { return stderrors.As(err, target) } func Unwrap(err error) error { return stderrors.Unwrap(err) } - -// Cause recursively unwraps an error chain and returns the underlying cause of -// the error, if possible. There are two ways that an error value may provide a -// cause. First, the error may implement the following interface: -// -// type causer interface { -// Cause() error -// } -// -// Second, the error may return a non-nil value when passed as an argument to -// the Unwrap function. This makes Cause forwards-compatible with Go 1.13 error -// chains. -// -// If an error value satisfies both methods of unwrapping, Cause will use the -// causer interface. -// -// 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 := Unwrap(err); unwrapped != nil { - err = unwrapped - } else { - break - } - } - return err -} diff --git a/go113_test.go b/go113_test.go index 7da3788..4ea37e6 100644 --- a/go113_test.go +++ b/go113_test.go @@ -9,29 +9,7 @@ import ( "testing" ) -func TestCauseErrorChainCompat(t *testing.T) { - err := stderrors.New("the cause!") - - // Wrap error using the standard library - wrapped := fmt.Errorf("wrapped with stdlib: %w", err) - if Cause(wrapped) != err { - t.Errorf("Cause does not support Go 1.13 error chains") - } - - // Wrap in another layer using pkg/errors - wrapped = WithMessage(wrapped, "wrapped with pkg/errors") - if Cause(wrapped) != err { - t.Errorf("Cause does not support Go 1.13 error chains") - } - - // Wrap in another layer using the standard library - wrapped = fmt.Errorf("wrapped with stdlib: %w", wrapped) - if Cause(wrapped) != err { - t.Errorf("Cause does not support Go 1.13 error chains") - } -} - -func TestWrapErrorChainCompat(t *testing.T) { +func TestErrorChainCompat(t *testing.T) { err := stderrors.New("error that gets wrapped") wrapped := Wrap(err, "wrapped up") if !stderrors.Is(wrapped, err) {