26 lines
574 B
Go
26 lines
574 B
Go
package recover
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"git.jeffthecoder.xyz/guochao/cache-proxy/pkgs/middleware/httplog"
|
|
)
|
|
|
|
func recoverer(w http.ResponseWriter, r *http.Request) {
|
|
if err := recover(); err != nil {
|
|
httplog.Logger(r).With("error", err).Error("request panicked")
|
|
http.Error(w, fmt.Sprint(err), http.StatusInternalServerError)
|
|
}
|
|
}
|
|
|
|
func Recover() func(http.Handler) http.Handler {
|
|
return func(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
defer recoverer(w, r)
|
|
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|
|
}
|