Files
lazyhandler/middleware/recover/recover.go
2025-06-18 10:12:19 +08:00

62 lines
1.7 KiB
Go

package recover
import (
"encoding/json"
"net/http"
"git.jeffthecoder.xyz/public/lazyhandler/middleware"
"git.jeffthecoder.xyz/public/lazyhandler/middleware/httplog"
)
type PanicResponseFunc func(http.ResponseWriter, *http.Request, any)
func recoverer(w http.ResponseWriter, r *http.Request, fn PanicResponseFunc) {
if err := recover(); err != nil {
httplog.Logger(r).With("panic", err).Error("request panicked")
if fn != nil {
fn(w, r, err)
} else {
w.WriteHeader(http.StatusInternalServerError)
}
}
}
func Recover(responseFunc PanicResponseFunc) middleware.Middleware {
return middleware.WrapFunc(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer recoverer(w, r, responseFunc)
next.ServeHTTP(w, r)
})
})
}
func DebugPanicHandler(w http.ResponseWriter, r *http.Request, err any) {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]any{
"error": err,
})
}
func Debug() middleware.Middleware {
return Recover(DebugPanicHandler)
}
// ProductionPanicHandler is a PanicResponseFunc that provides a generic error message
// in production environments and logs the detailed panic error.
func ProductionPanicHandler(w http.ResponseWriter, r *http.Request, err any) {
httplog.Logger(r).With("panic", err).Error("request panicked")
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(map[string]any{
"error": "Internal Server Error",
})
}
// Production returns a middleware that recovers from panics and handles them
// with ProductionPanicHandler.
func Production() middleware.Middleware {
return Recover(ProductionPanicHandler)
}