api/middlewares/recoverer.go
package middlewares
import (
"net/http"
"runtime/debug"
log "github.com/sirupsen/logrus"
)
// Recoverer is a middleware that recovers from panics, logs the panic (and a
// backtrace), and returns a HTTP 500 (Internal Server Error) status if possible.
func Recoverer(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
defer func() {
if rvr := recover(); rvr != nil && rvr != http.ErrAbortHandler {
log.WithFields(log.Fields{
"stacktrace": string(debug.Stack()),
}).Errorf("%+v", rvr)
w.WriteHeader(http.StatusInternalServerError)
}
}()
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}