From 0cab644e705fab8ed7804e4ed111c0a8b7616514 Mon Sep 17 00:00:00 2001 From: Matthew Barnes Date: Sat, 15 Feb 2025 11:02:18 -0500 Subject: [PATCH] frontend: Do not handle panics when running "go test" This effectively disables MiddlewarePanic when running unit tests. Catching a panic can sometimes obscure the root cause of a test failure, especially when the panic is intermittent and happens during a CI pipeline run where there's no opportunity to debug. This has been observed at least once before in https://github.com/Azure/ARO-HCP/pull/833 and may be happening again according to https://github.com/Azure/ARO-HCP/issues/1163 --- frontend/pkg/frontend/middleware_panic.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/frontend/pkg/frontend/middleware_panic.go b/frontend/pkg/frontend/middleware_panic.go index f594bbde9..8b6025c5c 100644 --- a/frontend/pkg/frontend/middleware_panic.go +++ b/frontend/pkg/frontend/middleware_panic.go @@ -7,18 +7,22 @@ import ( "fmt" "net/http" "runtime/debug" + "testing" "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 { - logger := LoggerFromContext(r.Context()) - logger.Error(fmt.Sprintf("panic: %#v\n%s\n", e, string(debug.Stack()))) - arm.WriteInternalServerError(w) - } - }() + // Do not catch panics when running "go test". + if !testing.Testing() { + defer func() { + if e := recover(); e != nil { + logger := LoggerFromContext(r.Context()) + logger.Error(fmt.Sprintf("panic: %#v\n%s\n", e, string(debug.Stack()))) + arm.WriteInternalServerError(w) + } + }() + } next(w, r) }