website/pkg/middleware/recovery.go

23 lines
509 B
Go
Raw Normal View History

package middleware
import (
"net/http"
"runtime/debug"
"git.kirsle.net/apps/gosocial/pkg/log"
)
// Recovery recovery middleware.
func Recovery(handler http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if err := recover(); err != nil {
log.Error("PANIC: %v", err)
debug.PrintStack()
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
}()
handler.ServeHTTP(w, r)
})
}