Skip to content

Commit c49782d

Browse files
Do not log context.Canceled error
ref #1556
2 parents acabb78 + cf41343 commit c49782d

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

pkg/lib/deps/middleware_request.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ func (m *RequestMiddleware) Handle(next http.Handler) http.Handler {
2626
appCtx, err := m.ConfigSource.ProvideContext(r)
2727
if err != nil {
2828
if errors.Is(err, configsource.ErrAppNotFound) {
29-
logger.WithError(err).Error("app not found")
3029
http.Error(w, configsource.ErrAppNotFound.Error(), http.StatusNotFound)
3130
} else {
3231
logger.WithError(err).Error("failed to resolve config")

pkg/util/log/ignore.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package log
2+
3+
import (
4+
"context"
5+
"errors"
6+
7+
"github.com/sirupsen/logrus"
8+
)
9+
10+
// Ignore reports whether the entry should be logged.
11+
func Ignore(entry *logrus.Entry) bool {
12+
if err, ok := entry.Data[logrus.ErrorKey].(error); ok {
13+
if errors.Is(err, context.Canceled) {
14+
return true
15+
}
16+
}
17+
18+
return false
19+
}

pkg/util/log/ignore_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package log
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"testing"
7+
8+
"github.com/sirupsen/logrus"
9+
10+
. "github.com/smartystreets/goconvey/convey"
11+
)
12+
13+
func TestIgnore(t *testing.T) {
14+
Convey("Ignore", t, func() {
15+
16+
Convey("Ignore entry with context.Canceled error", func() {
17+
logger := logrus.New()
18+
entry := logrus.NewEntry(logger)
19+
entry = entry.WithError(context.Canceled)
20+
21+
So(Ignore(entry), ShouldBeTrue)
22+
})
23+
24+
Convey("Ignore entry with wrapped context.Canceled error", func() {
25+
logger := logrus.New()
26+
entry := logrus.NewEntry(logger)
27+
28+
entry = entry.WithError(fmt.Errorf("wrap: %w", context.Canceled))
29+
30+
So(Ignore(entry), ShouldBeTrue)
31+
})
32+
33+
Convey("Do not ignore any other entry", func() {
34+
logger := logrus.New()
35+
entry := logrus.NewEntry(logger)
36+
37+
So(Ignore(entry), ShouldBeFalse)
38+
})
39+
})
40+
}

pkg/util/sentry/hook.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"github.com/getsentry/sentry-go"
88

99
"github.com/sirupsen/logrus"
10+
11+
"github.com/authgear/authgear-server/pkg/util/log"
1012
)
1113

1214
var LogHookLevels = []logrus.Level{
@@ -35,6 +37,10 @@ func (h *LogHook) Fire(entry *logrus.Entry) error {
3537
return nil
3638
}
3739

40+
if log.Ignore(entry) {
41+
return nil
42+
}
43+
3844
event := makeEvent(entry)
3945
h.hub.CaptureEvent(event)
4046
return nil

0 commit comments

Comments
 (0)