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 +} diff --git a/stacktrace_test.go b/stacktrace_test.go index c1276f2..ce6933d 100644 --- a/stacktrace_test.go +++ b/stacktrace_test.go @@ -98,3 +98,8 @@ func TestPropagateNil(t *testing.T) { assert.Equal(t, stacktrace.NoCode, stacktrace.GetCode(err)) } + +func TestUnwrap(t *testing.T) { + err := errors.New("cause") + 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() +}