gsave/controller/middleware/panic.go
Augusto Dwenger 2ae33c6e1b Add default middlewares
Add request logging middleware
Add panic recover middleware
2020-10-30 11:31:57 +01:00

18 lines
524 B
Go

package middleware
import "net/http"
// PanicRecoverHandler is a Handler to recover from any panic that happend down the handler chain.
// If a panic occurs it will generate an error log.
func PanicRecoverHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if err := recover(); err != nil {
logPackage.Errorf("Recovered from a panic: %+v\n", err)
http.Error(w, "Something went wrong.", 500)
}
}()
next.ServeHTTP(w, r)
})
}