mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-24 01:30:25 +08:00
23 lines
433 B
Go
23 lines
433 B
Go
package handler
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"runtime/debug"
|
|
|
|
"zero/rest/internal"
|
|
)
|
|
|
|
func RecoverHandler(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
defer func() {
|
|
if result := recover(); result != nil {
|
|
internal.Error(r, fmt.Sprintf("%v\n%s", result, debug.Stack()))
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
}
|
|
}()
|
|
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|