From 22020c850860e149fded80ce3fd4b3a59506d6eb Mon Sep 17 00:00:00 2001 From: Matthew Barnes Date: Tue, 16 Apr 2024 13:44:42 -0400 Subject: [PATCH] frontend: Log error when recovering from a panic Completes MiddlewarePanic, which was written before structured logging was added. --- frontend/middleware_panic.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/frontend/middleware_panic.go b/frontend/middleware_panic.go index ae7820fab..9d8514a45 100644 --- a/frontend/middleware_panic.go +++ b/frontend/middleware_panic.go @@ -4,7 +4,10 @@ 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" ) @@ -12,6 +15,9 @@ import ( 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) } }()