mirror of
https://github.com/hamburghammer/gsave.git
synced 2024-12-22 18:37:39 +01:00
2ae33c6e1b
Add request logging middleware Add panic recover middleware
18 lines
524 B
Go
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)
|
|
})
|
|
}
|