From 0e6f3a47f47c91f50acd2a1ddf15e88dcf34073d Mon Sep 17 00:00:00 2001 From: sam Date: Wed, 20 Sep 2023 15:15:43 +0200 Subject: [PATCH] fix(backend): filter out context.Canceled errors --- backend/server/errors.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/backend/server/errors.go b/backend/server/errors.go index 299ea63..063e773 100644 --- a/backend/server/errors.go +++ b/backend/server/errors.go @@ -1,10 +1,12 @@ package server import ( + "context" "fmt" "net/http" "codeberg.org/pronounscc/pronouns.cc/backend/log" + "emperror.dev/errors" "github.com/getsentry/sentry-go" "github.com/go-chi/chi/v5" "github.com/go-chi/render" @@ -37,9 +39,13 @@ func WrapHandler(hn func(w http.ResponseWriter, r *http.Request) error) http.Han scope.SetTag("path", rctx.RoutePattern()) }) - log.Errorf("error in handler for %v %v: %v", rctx.RouteMethod, rctx.RoutePattern(), err) - - eventID := hub.CaptureException(err) + var eventID *sentry.EventID = nil + if isExpectedError(err) { + log.Infof("expected error in handler for %v %v, ignoring", rctx.RouteMethod, rctx.RoutePattern()) + } else { + log.Errorf("error in handler for %v %v: %v", rctx.RouteMethod, rctx.RoutePattern(), err) + eventID = hub.CaptureException(err) + } apiErr := APIError{ID: eventID, Code: ErrInternalServerError} apiErr.prepare() @@ -49,6 +55,10 @@ func WrapHandler(hn func(w http.ResponseWriter, r *http.Request) error) http.Han } } +func isExpectedError(err error) bool { + return errors.Is(err, context.Canceled) +} + // APIError is an object returned by the API when an error occurs. // It implements the error interface and can be returned by handlers. type APIError struct {