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

feat: Support chained errors using Unwrap() #206

Merged
merged 1 commit into from
Apr 16, 2020
Merged
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
7 changes: 5 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,9 +328,12 @@ func (client *Client) eventFromException(exception error, level Level) *Event {
Type: reflect.TypeOf(err).String(),
Stacktrace: ExtractStacktrace(err),
})
if previous, ok := err.(interface{ Cause() error }); ok {
switch previous := err.(type) {
case interface{ Unwrap() error }:
err = previous.Unwrap()
case interface{ Cause() error }:
err = previous.Cause()
} else {
default:
err = nil
}
}
Expand Down
28 changes: 27 additions & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ func (e *customErrWithCause) Cause() error {
return e.cause
}

type wrappedError struct{ original error }

func (e wrappedError) Error() string {
return "wrapped: " + e.original.Error()
}

func (e wrappedError) Unwrap() error {
return e.original
}

type captureExceptionTestGroup struct {
name string
tests []captureExceptionTest
Expand Down Expand Up @@ -157,6 +167,21 @@ func TestCaptureException(t *testing.T) {
},
},
},
{
name: "Go113Unwrap",
err: wrappedError{original: errors.New("original")},
want: []Exception{
{
Type: "*errors.errorString",
Value: "original",
},
{
Type: "sentry.wrappedError",
Value: "wrapped: original",
Stacktrace: &Stacktrace{Frames: []Frame{}},
},
},
},
}

tests := []captureExceptionTestGroup{
Expand Down Expand Up @@ -222,7 +247,8 @@ func TestCaptureEvent(t *testing.T) {
Version: Version,
},
// TODO: perhaps the list of packages is incomplete or there
// should not be any package at all.
// should not be any package at all. We may include references
// to used integrations like http, echo, gin, etc.
},
},
}
Expand Down