feat: use latest go-chi v5, add middlewares for panic recovery and logging

Co-Authored-By: Sindre Rødseth Hansen <sindre.rodseth.hansen@nav.no>
This commit is contained in:
Trong Huu Nguyen
2021-10-05 10:55:08 +02:00
parent 70516c3efb
commit 77d0438411
13 changed files with 171 additions and 39 deletions

View File

@@ -1,22 +1,14 @@
package errorhandler
import (
"github.com/nais/wonderwall/pkg/middleware"
log "github.com/sirupsen/logrus"
"net/http"
"github.com/go-chi/httplog"
)
func respondError(w http.ResponseWriter, r *http.Request, statusCode int, cause error) {
id, ok := middleware.GetCorrelationID(r.Context())
if !ok {
log.Warnf("no correlation id in context")
}
logFields := log.Fields{
"correlation_id": id,
}
log.WithFields(logFields).Error(cause)
logger := httplog.LogEntry(r.Context())
logger.Error().Stack().Err(cause).Msgf("error in route: %+v", cause)
w.WriteHeader(statusCode)
}