neko/server/internal/http/middleware/recover.go

25 lines
496 B
Go
Raw Normal View History

2020-01-13 21:05:38 +13:00
package middleware
// The original work was derived from Goji's middleware, source:
// https://github.com/zenazn/goji/tree/master/web/middleware
import (
2020-01-19 12:30:09 +13:00
"net/http"
2020-01-13 21:05:38 +13:00
2020-01-19 12:30:09 +13:00
"n.eko.moe/neko/internal/http/endpoint"
2020-01-13 21:05:38 +13:00
)
func Recoverer(next http.Handler) http.Handler {
2020-01-19 12:30:09 +13:00
fn := func(w http.ResponseWriter, r *http.Request) {
defer func() {
if rvr := recover(); rvr != nil {
endpoint.WriteError(w, r, rvr)
}
}()
2020-01-13 21:05:38 +13:00
2020-01-19 12:30:09 +13:00
next.ServeHTTP(w, r)
}
2020-01-13 21:05:38 +13:00
2020-01-19 12:30:09 +13:00
return http.HandlerFunc(fn)
2020-01-13 21:05:38 +13:00
}