25 lines
471 B
Go
Raw Normal View History

2020-01-13 08:05:38 +00:00
package middleware
// The original work was derived from Goji's middleware, source:
// https://github.com/zenazn/goji/tree/master/web/middleware
import (
2021-04-04 22:37:33 +02:00
"net/http"
2020-01-13 08:05:38 +00:00
2021-10-05 22:38:24 +02:00
"m1k1o/neko/internal/http/endpoint"
2020-01-13 08:05:38 +00:00
)
func Recoverer(next http.Handler) http.Handler {
2021-04-04 22:37:33 +02:00
fn := func(w http.ResponseWriter, r *http.Request) {
defer func() {
if rvr := recover(); rvr != nil {
endpoint.WriteError(w, r, rvr)
}
}()
2020-01-13 08:05:38 +00:00
2021-04-04 22:37:33 +02:00
next.ServeHTTP(w, r)
}
2020-01-13 08:05:38 +00:00
2021-04-04 22:37:33 +02:00
return http.HandlerFunc(fn)
2020-01-13 08:05:38 +00:00
}