Some checks failed
Deploy / deploy (push) Has been cancelled
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>
36 lines
664 B
Go
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)
|
|
})
|
|
}
|