1 Star 0 Fork 0

码布什 / go工具库

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
CatchException.go 1.32 KB
一键复制 编辑 原始数据 按行查看 历史
zhengqiuyun 提交于 2024-01-12 11:21 . 默认开启日志清理
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"
"strings"
)
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", transformError(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)
}
func transformError(err string) string {
if len(err) > 0 {
if strings.Contains(err, "Duplicate entry") {
k := "Duplicate entry "
starIdx := strings.Index(err, k)
endIdx := strings.Index(err, " for key")
return err[len(k)+starIdx:endIdx] + "重复"
}
}
return err
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/manoshi/go-util.git
git@gitee.com:manoshi/go-util.git
manoshi
go-util
go工具库
v0.1.5

搜索帮助

344bd9b3 5694891 D2dac590 5694891