2 Star 0 Fork 0

码布什/go工具库

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
CatchException.go 1.01 KB
一键复制 编辑 原始数据 按行查看 历史
zhengqiuyun 提交于 2023-02-21 16:29 +08:00 . Init git
package catch
import (
"fmt"
"gitee.com/manoshi/go-util/exception"
"gitee.com/manoshi/go-util/getWay"
"gitee.com/manoshi/go-util/log"
"github.com/gin-gonic/gin"
"runtime"
)
func ExceptionCatch(c *gin.Context) {
err := recover() //获取异常
if err != nil {
switch err.(type) {
case exception.DError:
e := (err).(exception.DError)
if c == nil {
log.Errorf("%s-%s", e.Code, e.Err)
} else {
getWay.FailAndMsg(c, e.Code, e.Err)
}
break
case error:
e := (err).(error)
if c == nil {
log.Error(e.Error())
} else {
getWay.FailAndMsg(c, "SYSTEM.ERROR", e.Error())
}
printStack()
break
case string:
e := (err).(string)
if c == nil {
log.Error(e)
} else {
getWay.FailAndMsg(c, "SYSTEM.ERROR", e)
}
printStack()
break
default:
if c != nil {
getWay.FailAndMsg(c, "SYSTEM.ERROR", "系统繁忙...")
}
printStack()
break
}
}
}
func printStack() {
var buf [1024]byte
n := runtime.Stack(buf[:], true)
fmt.Println(string(buf[:]), n)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/manoshi/go-util.git
git@gitee.com:manoshi/go-util.git
manoshi
go-util
go工具库
v0.0.27

搜索帮助