28 lines
587 B
Go
28 lines
587 B
Go
package recovery
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httputil"
|
|
"runtime/debug"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
func GinRecoverMiddleware(logger *zap.SugaredLogger) gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
defer func() {
|
|
if err := recover(); err != nil {
|
|
debug.PrintStack()
|
|
httprequest, _ := httputil.DumpRequest(c.Request, false)
|
|
logger.Errorf("uri:%s,request:%s", c.Request.RequestURI, string(httprequest))
|
|
|
|
c.String(http.StatusInternalServerError, http.StatusText(http.StatusInternalServerError))
|
|
c.Abort()
|
|
}
|
|
}()
|
|
|
|
c.Next()
|
|
}
|
|
}
|