Files
cv-site/internal/middleware/recovery.go
T

25 lines
558 B
Go
Raw Normal View History

2025-10-20 08:54:21 +01:00
package middleware
import (
"log"
"net/http"
"runtime/debug"
)
// Recovery recovers from panics and logs the error with stack trace
func Recovery(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if err := recover(); err != nil {
// Log the panic with stack trace
log.Printf("PANIC: %v\n%s", err, debug.Stack())
// Return 500 Internal Server Error
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
}
}()
next.ServeHTTP(w, r)
})
}