Skip to content

Commit 78509ec

Browse files
committed
feat: Extract correct type and value from top-most error
1 parent 383614e commit 78509ec

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

Diff for: client.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,17 @@ func (client *Client) eventFromException(exception error, level Level) *Event {
298298
stacktrace = NewStacktrace()
299299
}
300300

301+
cause := exception
302+
// Handle wrapped errors for github.com/pingcap/errors and github.com/pkg/errors
303+
if ex, ok := exception.(interface{ Cause() error }); ok {
304+
cause = ex.Cause()
305+
}
306+
301307
event := NewEvent()
302308
event.Level = level
303309
event.Exception = []Exception{{
304-
Value: exception.Error(),
305-
Type: reflect.TypeOf(exception).String(),
310+
Value: cause.Error(),
311+
Type: reflect.TypeOf(cause).String(),
306312
Stacktrace: stacktrace,
307313
}}
308314
return event

Diff for: client_test.go

+18
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ package sentry
33
import (
44
"errors"
55
"testing"
6+
7+
pkgErrors "github.com/pkg/errors"
68
)
79

810
func TestNewClientAllowsEmptyDSN(t *testing.T) {
@@ -58,6 +60,7 @@ func TestCaptureMessageShouldSucceedWithoutNilScope(t *testing.T) {
5860
func TestCaptureExceptionShouldSendEventWithProvidedError(t *testing.T) {
5961
client, scope, transport := setupClientTest()
6062
client.CaptureException(errors.New("custom error"), nil, scope)
63+
assertEqual(t, transport.lastEvent.Exception[0].Type, "*errors.errorString")
6164
assertEqual(t, transport.lastEvent.Exception[0].Value, "custom error")
6265
}
6366

@@ -67,6 +70,21 @@ func TestCaptureExceptionShouldNotFailWhenPassedNil(t *testing.T) {
6770
assertEqual(t, transport.lastEvent.Message, "Called CaptureException with nil value")
6871
}
6972

73+
type customErr struct{}
74+
75+
func (e *customErr) Error() string {
76+
return "wat"
77+
}
78+
79+
func TestCaptureExceptionShouldExtractCorrectTypeAndValueForWrappedErrors(t *testing.T) {
80+
client, scope, transport := setupClientTest()
81+
cause := &customErr{}
82+
err := pkgErrors.WithStack(cause)
83+
client.CaptureException(err, nil, scope)
84+
assertEqual(t, transport.lastEvent.Exception[0].Type, "*sentry.customErr")
85+
assertEqual(t, transport.lastEvent.Exception[0].Value, "wat")
86+
}
87+
7088
func TestCaptureEventShouldSendEventWithProvidedError(t *testing.T) {
7189
client, scope, transport := setupClientTest()
7290
event := NewEvent()

0 commit comments

Comments
 (0)