gitlab.com/gitlab-org/labkit@v1.21.0/errortracking/sentry_tracker_test.go (about) 1 package errortracking 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/getsentry/sentry-go" 8 "github.com/stretchr/testify/require" 9 ) 10 11 func TestCaptureWithStackTrace(t *testing.T) { 12 innerError := fmt.Errorf("inner error") 13 midError := fmt.Errorf("mid error: %w", innerError) 14 err := fmt.Errorf("top error: %w", midError) 15 16 tcs := map[string]struct { 17 withStackTrace bool 18 expectedExceptionsLength int 19 }{ 20 "with_stack_trace": { 21 withStackTrace: true, 22 expectedExceptionsLength: 3, 23 }, 24 "without_stack_trace": { 25 withStackTrace: false, 26 expectedExceptionsLength: 1, 27 }, 28 } 29 30 for tn, tc := range tcs { 31 t.Run(tn, func(t *testing.T) { 32 sm := &sentryMock{} 33 tracker := newSentryTracker(sm) 34 35 if tc.withStackTrace { 36 tracker.Capture(err, WithStackTrace()) 37 require.Len(t, sm.event.Exception, tc.expectedExceptionsLength) 38 39 // exceptions are reversed so that the inner-most error is reported first 40 require.Equal(t, innerError.Error(), sm.event.Exception[0].Value) 41 require.Equal(t, midError.Error(), sm.event.Exception[1].Value) 42 require.Equal(t, err.Error(), sm.event.Exception[2].Value) 43 } else { 44 tracker.Capture(err) 45 46 require.Len(t, sm.event.Exception, tc.expectedExceptionsLength) 47 require.Equal(t, err.Error(), sm.event.Exception[0].Value, "must match top error") 48 } 49 }) 50 } 51 } 52 53 type sentryMock struct { 54 event *sentry.Event 55 } 56 57 func (sm *sentryMock) CaptureEvent(event *sentry.Event) *sentry.EventID { 58 sm.event = event 59 60 return &event.EventID 61 }