Skip to content

Commit

Permalink
Merge pull request #56 from mbarnes/middleware-panic-log-error
Browse files Browse the repository at this point in the history
frontend: Log error when recovering from a panic
  • Loading branch information
bennerv authored Apr 16, 2024
2 parents 3382078 + 22020c8 commit 7d6dd5c
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions frontend/middleware_panic.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,20 @@ package main
// Licensed under the Apache License 2.0.

import (
"fmt"
"log/slog"
"net/http"
"runtime/debug"

"github.com/Azure/ARO-HCP/internal/api/arm"
)

func MiddlewarePanic(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
defer func() {
if e := recover(); e != nil {
if logger, ok := r.Context().Value(ContextKeyLogger).(*slog.Logger); ok {
logger.Error(fmt.Sprintf("panic: %#v\n%s\n", e, string(debug.Stack())))
}
arm.WriteInternalServerError(w)
}
}()
Expand Down

0 comments on commit 7d6dd5c

Please sign in to comment.