From 5bb7de2dc09864d3e478a0cb2f558d5be1168d9a Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Thu, 27 Aug 2020 13:47:33 +0200 Subject: [PATCH 1/4] Add Unwrap support --- stacktrace.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/stacktrace.go b/stacktrace.go index 324c4bc..b12baa2 100644 --- a/stacktrace.go +++ b/stacktrace.go @@ -247,3 +247,8 @@ func (st *stacktrace) ExitCode() int { } return int(st.code) } + +// Unwrap returns the cause of st. +func (st *stacktrace) Unwrap() error { + return st.cause +} From 4aedc35fc2da2e2bd459dc2766fa51fcdc3d8f9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Vo=C3=9F?= Date: Thu, 27 Aug 2020 13:54:42 +0200 Subject: [PATCH 2/4] Add Unwrap test --- stacktrace_test.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/stacktrace_test.go b/stacktrace_test.go index c1276f2..86d00e2 100644 --- a/stacktrace_test.go +++ b/stacktrace_test.go @@ -98,3 +98,15 @@ func TestPropagateNil(t *testing.T) { assert.Equal(t, stacktrace.NoCode, stacktrace.GetCode(err)) } + +func TestUnwrap(t *testing.T) { + st := &stacktrace{} + assert.Nil(t, st.Unwrap()) + + err := errors.New("cause") + st.cause = err + assert.Equal(t, err, st.Unwrap()) + + st = Propagate(err, "propagated") + assert.Equal(t, err, st.Unwrap()) +} From ab0e18e17435b7862b9ab6898c215e4e6353582d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Vo=C3=9F?= Date: Thu, 27 Aug 2020 14:16:32 +0200 Subject: [PATCH 3/4] Add support for all Go versions in test --- stacktrace_test.go | 9 +-------- unwrap_1_13_test.go | 9 +++++++++ unwrap_test.go | 11 +++++++++++ 3 files changed, 21 insertions(+), 8 deletions(-) create mode 100644 unwrap_1_13_test.go create mode 100644 unwrap_test.go diff --git a/stacktrace_test.go b/stacktrace_test.go index 86d00e2..ce6933d 100644 --- a/stacktrace_test.go +++ b/stacktrace_test.go @@ -100,13 +100,6 @@ func TestPropagateNil(t *testing.T) { } func TestUnwrap(t *testing.T) { - st := &stacktrace{} - assert.Nil(t, st.Unwrap()) - err := errors.New("cause") - st.cause = err - assert.Equal(t, err, st.Unwrap()) - - st = Propagate(err, "propagated") - assert.Equal(t, err, st.Unwrap()) + assert.Equal(t, err, unwrap(stacktrace.Propagate(err, "propagated"))) } diff --git a/unwrap_1_13_test.go b/unwrap_1_13_test.go new file mode 100644 index 0000000..82b412a --- /dev/null +++ b/unwrap_1_13_test.go @@ -0,0 +1,9 @@ +// +build go1.13 + +package stacktrace_test + +import "errors" + +func unwrap(err error) error { + return errors.Unwrap(err) +} diff --git a/unwrap_test.go b/unwrap_test.go new file mode 100644 index 0000000..cef3dae --- /dev/null +++ b/unwrap_test.go @@ -0,0 +1,11 @@ +// +build !go1.13 + +package stacktrace_test + +func unwrap(err error) error { + t, ok := err.(interface{ Unwrap() error }) + if !ok { + return nil + } + return t.Unwrap() +} From b2e58cf4881875ca3d11c0510af522803aa8f259 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Vo=C3=9F?= Date: Thu, 27 Aug 2020 20:00:54 +0200 Subject: [PATCH 4/4] Kick CI