Skip to content

Commit

Permalink
fix(core): handle security handler errors in error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
ayuhito committed Jun 12, 2024
1 parent 602bb47 commit 9654503
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions core/middlewares/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/go-faster/jx"
"github.com/medama-io/medama/model"
"github.com/medama-io/medama/util/logger"
"github.com/ogen-go/ogen/ogenerrors"
)
Expand All @@ -16,17 +17,21 @@ func ErrorHandler(ctx context.Context, w http.ResponseWriter, req *http.Request,
code := ogenerrors.ErrorCode(err)
errMessage := strings.ReplaceAll(err.Error(), "\"", "'")

log := logger.Get()
log.Error().
log := logger.Get().With().
Str("path", req.URL.Path).
Str("method", req.Method).
Int("status_code", code).
Str("message", errMessage).
Str("Connection", req.Header.Get("Connection")).
Str("Content-Type", req.Header.Get("Content-Type")).
Str("Content-Length", req.Header.Get("Content-Length")).
Str("User-Agent", req.Header.Get("User-Agent")).
Msg("error 500")
Str("User-Agent", req.Header.Get("User-Agent")).Logger()

if errors.Is(err, model.ErrUnauthorised) || code == http.StatusUnauthorized {
log.Warn().Msg("unauthorised")
} else {
log.Error().Msg(err.Error())
}

if errors.Is(err, ogenerrors.ErrSecurityRequirementIsNotSatisfied) {
errMessage = "missing security token or cookie"
Expand Down

0 comments on commit 9654503

Please sign in to comment.