Ai
1 Star 0 Fork 1

雅痞/go-xxljob-executor

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
recovery.go 1.32 KB
一键复制 编辑 原始数据 按行查看 历史
yangzhiqiang02 提交于 2023-03-01 09:52 +08:00 . 自定义
package server
import (
"bytes"
"fmt"
"gitee.com/YaPi98/go-xxljob-executor/biz/model"
"github.com/gin-gonic/gin"
"net/http"
"runtime"
"strings"
)
func Recovery(c *gin.Context) {
defer func() {
if r := recover(); r != nil {
//打印错误堆栈信息
//logger.GetLogger().Errorf("panic: %v\n", r)
//s := printStackTrace(r)
//Result.Fail不是本例的重点,因此用下面代码代替
c.JSON(http.StatusOK, model.ReturnT{
Code: model.FAIL_CODE,
Msg: fmt.Sprintf("panic: %v\n", r),
})
//终止后续接口调用,不加的话recover到异常后,还会继续执行接口里后续代码
c.Abort()
}
}()
//加载完 defer recover,继续后续接口调用
c.Next()
}
// 打印堆栈信息
func printStackTrace(err interface{}) string {
buf := new(bytes.Buffer)
fmt.Fprintf(buf, "%v\n", err)
for i := 1; ; i++ {
pc, file, line, ok := runtime.Caller(i)
if !ok {
break
}
if strings.Contains(file, "/go/") || strings.Contains(file, "/middleware/recovery.go") || strings.Contains(file, "/middleware/intercepter.go") {
continue
}
fmt.Fprintf(buf, "%s:%d (0x%x)\n", file, line, pc)
}
return buf.String()
}
func Deal404(c *gin.Context) {
c.JSON(http.StatusOK, model.ReturnT{
Code: model.FAIL_CODE,
Msg: fmt.Sprintf("path %s dose not exist!", c.FullPath()),
})
c.Abort()
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/YaPi98/go-xxljob-executor.git
git@gitee.com:YaPi98/go-xxljob-executor.git
YaPi98
go-xxljob-executor
go-xxljob-executor
b025b398d5e8

搜索帮助