Skip to content

Commit 372ff36

Browse files
authored
Merge pull request #3 from aereal/consider-problematic-status
feat: add ConsiderProblemStatusCode option
2 parents b582676 + 066d65c commit 372ff36

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

sentry/handler.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ type Options struct {
3434

3535
// FlushTimeout for the delivery events. Defaults to 2s. Only relevant when WaitForDelivery is true.
3636
FlushTimeout time.Duration
37+
38+
// ConsiderProblematicStatusCode tells the middleware what status code will have the problem details.
39+
//
40+
// Defaults to 500-599 considered problematic status code.
41+
ConsiderProblematicStatusCode func(statusCode int) bool
42+
}
43+
44+
var OnlyServerError = func(statusCode int) bool {
45+
return statusCode >= 500 && statusCode < 600
3746
}
3847

3948
// New returns new middleware that reports problems to Sentry.
@@ -46,12 +55,16 @@ func New(opts Options) httputil.Middleware {
4655
if timeout == 0 {
4756
timeout = time.Second * 2
4857
}
58+
check := opts.ConsiderProblematicStatusCode
59+
if check == nil {
60+
check = OnlyServerError
61+
}
4962
return func(next http.Handler) http.Handler {
5063
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
5164
buf := new(bytes.Buffer)
5265
tw := httputil.NewTeeResponseWriter(rw, buf)
5366
next.ServeHTTP(tw, r)
54-
if !isValidContentType(tw.Header().Get("content-type")) {
67+
if !(isValidContentType(tw.Header().Get("content-type")) && check(tw.StatusCode())) {
5568
return
5669
}
5770
hub := sentrysdk.GetHubFromContext(r.Context())

sentry/handler_test.go

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ func Test_ok(t *testing.T) {
2020
withInstanceProblem.Instance = "http://instance.example/"
2121

2222
testCases := []struct {
23-
name string
24-
problem *problems.DefaultProblem
25-
wantEvents []*sentrysdk.Event
23+
name string
24+
problem *problems.DefaultProblem
25+
wantEvents []*sentrysdk.Event
26+
considerProblem func(statusCode int) bool
2627
}{
2728
{
2829
"minimal",
@@ -36,6 +37,7 @@ func Test_ok(t *testing.T) {
3637
},
3738
},
3839
},
40+
nil,
3941
},
4042
{
4143
"with detail",
@@ -50,6 +52,7 @@ func Test_ok(t *testing.T) {
5052
},
5153
},
5254
},
55+
nil,
5356
},
5457
{
5558
"with instance",
@@ -64,6 +67,29 @@ func Test_ok(t *testing.T) {
6467
},
6568
},
6669
},
70+
nil,
71+
},
72+
{
73+
"client error",
74+
problems.NewStatusProblem(http.StatusBadRequest),
75+
nil,
76+
nil,
77+
},
78+
{
79+
"capture client error",
80+
problems.NewStatusProblem(http.StatusBadRequest),
81+
[]*sentrysdk.Event{
82+
{
83+
Level: sentrysdk.LevelInfo,
84+
Message: http.StatusText(http.StatusBadRequest),
85+
Extra: map[string]interface{}{
86+
"problem.status": http.StatusBadRequest,
87+
},
88+
},
89+
},
90+
func(statusCode int) bool {
91+
return statusCode >= 400 && statusCode < 500
92+
},
6793
},
6894
}
6995
for _, tc := range testCases {
@@ -74,7 +100,7 @@ func Test_ok(t *testing.T) {
74100
}
75101
problems.StatusProblemHandler(tc.problem).ServeHTTP(rw, r)
76102
})
77-
srv := httptest.NewServer(withSentryHub()(New(Options{WaitForDelivery: true})(h)))
103+
srv := httptest.NewServer(withSentryHub()(New(Options{WaitForDelivery: true, ConsiderProblematicStatusCode: tc.considerProblem})(h)))
78104
defer srv.Close()
79105

80106
var mux sync.Mutex

0 commit comments

Comments
 (0)