Files
terminalShop-old/api/middleware/recovery.go
marcusDenslow ae7eaa8e97
Some checks failed
Deploy / deploy (push) Has been cancelled
feat: route audit + recovery through slog levels
Audit events now emit at Info/Warn/Error so the log-level counter has variety
instead of being pinned to info. Panic recovery uses structured slog.Error
with method/path/stack attrs.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-01 13:55:54 +02:00

36 lines
664 B
Go

package middleware
import (
"log/slog"
"net/http"
"runtime/debug"
"terminalShop/pkg/utils"
)
// Recovery recovers from panics and returns a 500 error
func Recovery(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if err := recover(); err != nil {
slog.Error("panic recovered",
"error", err,
"method", r.Method,
"path", r.URL.Path,
"stack", string(debug.Stack()),
)
utils.RespondError(
w,
http.StatusInternalServerError,
"INTERNAL_ERROR",
"an unexpected error occurred",
nil,
)
}
}()
next.ServeHTTP(w, r)
})
}