mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-10 08:29:58 +08:00
23 lines
435 B
Go
23 lines
435 B
Go
|
|
package httphandler
|
||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
"net/http"
|
||
|
|
"runtime/debug"
|
||
|
|
|
||
|
|
"zero/core/httplog"
|
||
|
|
)
|
||
|
|
|
||
|
|
func RecoverHandler(next http.Handler) http.Handler {
|
||
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||
|
|
defer func() {
|
||
|
|
if result := recover(); result != nil {
|
||
|
|
httplog.Error(r, fmt.Sprintf("%v\n%s", result, debug.Stack()))
|
||
|
|
w.WriteHeader(http.StatusInternalServerError)
|
||
|
|
}
|
||
|
|
}()
|
||
|
|
|
||
|
|
next.ServeHTTP(w, r)
|
||
|
|
})
|
||
|
|
}
|