From 7427754f3f84d42bf96ac906cb1ab85ce4193268 Mon Sep 17 00:00:00 2001 From: Jay Weisskopf Date: Wed, 3 Jul 2019 15:15:56 -0400 Subject: [PATCH 1/3] Add support for Go 1.13 error chains Go 1.13 adds support for error chains to the standard libary's errors package. The new standard library functions require an Unwrap method to be provided by an error type. This change adds a new Unwrap method (identical to the existing Cause method) to the unexported error types. --- errors.go | 6 ++++++ go1.13_compat_test.go | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 go1.13_compat_test.go diff --git a/errors.go b/errors.go index 8617bee..161aea2 100644 --- a/errors.go +++ b/errors.go @@ -159,6 +159,9 @@ type withStack struct { func (w *withStack) Cause() error { return w.error } +// Unwrap provides compatibility for Go 1.13 error chains. +func (w *withStack) Unwrap() error { return w.error } + func (w *withStack) Format(s fmt.State, verb rune) { switch verb { case 'v': @@ -241,6 +244,9 @@ type withMessage struct { func (w *withMessage) Error() string { return w.msg + ": " + w.cause.Error() } func (w *withMessage) Cause() error { return w.cause } +// Unwrap provides compatibility for Go 1.13 error chains. +func (w *withMessage) Unwrap() error { return w.cause } + func (w *withMessage) Format(s fmt.State, verb rune) { switch verb { case 'v': diff --git a/go1.13_compat_test.go b/go1.13_compat_test.go new file mode 100644 index 0000000..3f40fa8 --- /dev/null +++ b/go1.13_compat_test.go @@ -0,0 +1,18 @@ +// +build go1.13 + +package errors_test + +import ( + stdlib_errors "errors" + "testing" + + "github.com/pkg/errors" +) + +func TestErrorChainCompat(t *testing.T) { + err := stdlib_errors.New("error that gets wrapped") + wrapped := errors.Wrap(err, "wrapped up") + if !stdlib_errors.Is(wrapped, err) { + t.Errorf("Wrap does not support Go 1.13 error chains") + } +} From 3958db8e5abf17c494ba3833855c4b7987df2d01 Mon Sep 17 00:00:00 2001 From: Jay Petacat Date: Fri, 1 Nov 2019 19:03:43 -0400 Subject: [PATCH 2/3] Rename go1.13_compat_test.go to go113_test.go --- go1.13_compat_test.go => go113_test.go | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename go1.13_compat_test.go => go113_test.go (100%) diff --git a/go1.13_compat_test.go b/go113_test.go similarity index 100% rename from go1.13_compat_test.go rename to go113_test.go From afdfa07cbc5f45147ee8378b9b57e58dbeb0f6c5 Mon Sep 17 00:00:00 2001 From: Jay Petacat Date: Sat, 9 Nov 2019 04:38:58 -0500 Subject: [PATCH 3/3] Move go113_test back into errors package --- go113_test.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/go113_test.go b/go113_test.go index 3f40fa8..39263b0 100644 --- a/go113_test.go +++ b/go113_test.go @@ -1,17 +1,15 @@ // +build go1.13 -package errors_test +package errors import ( stdlib_errors "errors" "testing" - - "github.com/pkg/errors" ) func TestErrorChainCompat(t *testing.T) { err := stdlib_errors.New("error that gets wrapped") - wrapped := errors.Wrap(err, "wrapped up") + wrapped := Wrap(err, "wrapped up") if !stdlib_errors.Is(wrapped, err) { t.Errorf("Wrap does not support Go 1.13 error chains") }