Skip to content

Commit

Permalink
Further reduce noise #4205
Browse files Browse the repository at this point in the history
ref DEV-1251
  • Loading branch information
tung2744 authored May 8, 2024
2 parents 1c4177b + e234942 commit 63c9352
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 8 deletions.
14 changes: 8 additions & 6 deletions pkg/api/apierrors/skip_logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package apierrors
import (
"context"
"database/sql"
"encoding/json"
"errors"
"net/http"
"syscall"

"github.com/lib/pq"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -53,6 +53,13 @@ func IgnoreError(err error) (ignore bool) {
ignore = true
}

// json.Unmarshal returns a SyntaxError if the JSON can't be parsed.
// https://pkg.go.dev/encoding/json#SyntaxError
var jsonSyntaxError *json.SyntaxError
if errors.As(err, &jsonSyntaxError) {
ignore = true
}

var pqError *pq.Error
if errors.As(err, &pqError) {
// https://www.postgresql.org/docs/13/errcodes-appendix.html
Expand All @@ -62,11 +69,6 @@ func IgnoreError(err error) (ignore bool) {
}
}

if errors.Is(err, syscall.EPIPE) {
// syscall.EPIPE is "write: broken pipe"
ignore = true
}

if errors.Is(err, sql.ErrTxDone) {
// The sql package will rollback the transaction when the context is canceled.
// https://pkg.go.dev/database/sql/driver#ConnBeginTx
Expand Down
55 changes: 53 additions & 2 deletions pkg/api/apierrors/skip_logging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package apierrors

import (
"context"
"database/sql"
"encoding/json"
"fmt"
"net/http"
"syscall"
"testing"

"github.com/lib/pq"
"github.com/sirupsen/logrus"

. "github.com/smartystreets/goconvey/convey"

"github.com/authgear/authgear-server/pkg/util/log"
Expand Down Expand Up @@ -38,7 +42,54 @@ func TestSkipLogging(t *testing.T) {
So(myhook.IsSkipped, ShouldBeTrue)
})

Convey("Ignore entry with wrapped context.Canceled error", func() {
Convey("Ignore entry with context.DeadlineExceeded error", func() {
logger.WithError(context.DeadlineExceeded).Error("error")
So(myhook.IsSkipped, ShouldBeTrue)
})

Convey("Ignore entry with http.ErrAbortHandler error", func() {
logger.WithError(http.ErrAbortHandler).Error("error")
So(myhook.IsSkipped, ShouldBeTrue)
})

Convey("Ignore json.SyntaxError", func() {
var anything interface{}
err := json.Unmarshal([]byte("{"), &anything)
So(err, ShouldBeError, "unexpected end of JSON input")

logger.WithError(err).Error("error")
So(myhook.IsSkipped, ShouldBeTrue)
})

Convey("Ignore pg.Error.Code 57014", func() {
err := &pq.Error{
Code: "57014",
}

logger.WithError(err).Error("error")
So(myhook.IsSkipped, ShouldBeTrue)
})

Convey("Do not ignore syscall.EPIPE", func() {
err := syscall.EPIPE
logger.WithError(err).Error("error")
So(err, ShouldBeError, "broken pipe")
So(myhook.IsSkipped, ShouldBeFalse)
})

Convey("Do not ignore syscall.ECONNREFUSED", func() {
err := syscall.ECONNREFUSED
logger.WithError(err).Error("error")
So(err, ShouldBeError, "connection refused")
So(myhook.IsSkipped, ShouldBeFalse)
})

Convey("Ignore sql.ErrTxDone error", func() {
logger.WithError(sql.ErrTxDone).Error("error")
So(myhook.IsSkipped, ShouldBeTrue)
})

Convey("Ignore entry with wrapped error", func() {
logger.WithError(fmt.Errorf("wrap: %w", context.Canceled)).Error("error")
So(myhook.IsSkipped, ShouldBeTrue)
})
Expand Down

0 comments on commit 63c9352

Please sign in to comment.