go-zero/rest/handler/recoverhandler.go

23 lines
433 B
Go
Raw Normal View History

2020-07-29 18:00:04 +08:00
package handler
2020-07-26 17:09:05 +08:00
import (
"fmt"
"net/http"
"runtime/debug"
2020-07-31 11:14:48 +08:00
"zero/rest/internal"
2020-07-26 17:09:05 +08:00
)
func RecoverHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
defer func() {
if result := recover(); result != nil {
2020-07-29 18:00:04 +08:00
internal.Error(r, fmt.Sprintf("%v\n%s", result, debug.Stack()))
2020-07-26 17:09:05 +08:00
w.WriteHeader(http.StatusInternalServerError)
}
}()
next.ServeHTTP(w, r)
})
}