File: sentry_tracker_test.go

package info (click to toggle)
golang-gitlab-gitlab-org-labkit 1.17.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,092 kB
  • sloc: sh: 210; javascript: 49; makefile: 4
file content (61 lines) | stat: -rw-r--r-- 1,522 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package errortracking

import (
	"fmt"
	"testing"

	"github.com/getsentry/sentry-go"
	"github.com/stretchr/testify/require"
)

func TestCaptureWithStackTrace(t *testing.T) {
	innerError := fmt.Errorf("inner error")
	midError := fmt.Errorf("mid error: %w", innerError)
	err := fmt.Errorf("top error: %w", midError)

	tcs := map[string]struct {
		withStackTrace           bool
		expectedExceptionsLength int
	}{
		"with_stack_trace": {
			withStackTrace:           true,
			expectedExceptionsLength: 3,
		},
		"without_stack_trace": {
			withStackTrace:           false,
			expectedExceptionsLength: 1,
		},
	}

	for tn, tc := range tcs {
		t.Run(tn, func(t *testing.T) {
			sm := &sentryMock{}
			tracker := newSentryTracker(sm)

			if tc.withStackTrace {
				tracker.Capture(err, WithStackTrace())
				require.Len(t, sm.event.Exception, tc.expectedExceptionsLength)

				// exceptions are reversed so that the inner-most error is reported first
				require.Equal(t, innerError.Error(), sm.event.Exception[0].Value)
				require.Equal(t, midError.Error(), sm.event.Exception[1].Value)
				require.Equal(t, err.Error(), sm.event.Exception[2].Value)
			} else {
				tracker.Capture(err)

				require.Len(t, sm.event.Exception, tc.expectedExceptionsLength)
				require.Equal(t, err.Error(), sm.event.Exception[0].Value, "must match top error")
			}
		})
	}
}

type sentryMock struct {
	event *sentry.Event
}

func (sm *sentryMock) CaptureEvent(event *sentry.Event) *sentry.EventID {
	sm.event = event

	return &event.EventID
}