Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unwrap support #14

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions stacktrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
5 changes: 5 additions & 0 deletions stacktrace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")))
}
9 changes: 9 additions & 0 deletions unwrap_1_13_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// +build go1.13

package stacktrace_test

import "errors"

func unwrap(err error) error {
return errors.Unwrap(err)
}
11 changes: 11 additions & 0 deletions unwrap_test.go
Original file line number Diff line number Diff line change
@@ -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()
}