23 lines
519 B
Go
23 lines
519 B
Go
|
|
package middleware
|
||
|
|
|
||
|
|
import (
|
||
|
|
"log"
|
||
|
|
"net/http"
|
||
|
|
)
|
||
|
|
|
||
|
|
// Recovery is a middleware that recovers from any panics and writes a 500 if there was one
|
||
|
|
func Recovery() Middleware {
|
||
|
|
return func(next http.Handler) http.Handler {
|
||
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
|
defer func() {
|
||
|
|
if err := recover(); err != nil {
|
||
|
|
log.Printf("PANIC RECOVERED: %v", err)
|
||
|
|
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||
|
|
}
|
||
|
|
}()
|
||
|
|
|
||
|
|
next.ServeHTTP(w, r)
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|