36 lines
520 B
Go
36 lines
520 B
Go
package middleware
|
|
|
|
import (
|
|
"github.com/gin-gonic/gin"
|
|
"net/http"
|
|
"runtime/debug"
|
|
)
|
|
|
|
func Recover(c *gin.Context) {
|
|
defer func() {
|
|
if r := recover(); r != nil {
|
|
debug.PrintStack()
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 500,
|
|
"message": "系统异常",
|
|
"detailError": errorToString(r),
|
|
"data": nil,
|
|
})
|
|
|
|
c.Abort()
|
|
}
|
|
}()
|
|
|
|
c.Next()
|
|
}
|
|
|
|
func errorToString(r interface{}) string {
|
|
switch v := r.(type) {
|
|
case error:
|
|
return v.Error()
|
|
default:
|
|
return r.(string)
|
|
}
|
|
}
|