mirror of
https://github.com/hamburghammer/gsave.git
synced 2024-12-22 18:37:39 +01:00
19 lines
524 B
Go
19 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)
|
||
|
})
|
||
|
}
|