1 Star 4 Fork 2

tym_hmm/go-helper

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Exception.go 1.35 KB
一键复制 编辑 原始数据 按行查看 历史
天蝎儿 提交于 2022-11-08 23:21 . 修改throw 是否显示日志输出
package ExceptionHelper
import (
"gitee.com/tym_hmm/hlog"
)
type Exception struct {
Code int
Message string
}
type ExceptionHandler func(Exception)
type ExceptionStruct struct {
catch ExceptionHandler
finally func()
try func()
}
//抛异常
func ThrowLog(code int, message string, param ...interface{}) {
throwException(code, message, true, param)
}
func Throw(code int, message string, param ...interface{}) {
throwException(code, message, false, param)
}
func throwException(code int, message string, isLog bool, param ...interface{}) {
if param != nil && isLog {
hlog.NewStdLog().Error("code:%d,\nmessage: %s,\nerr:%v", code, message, param)
}
panic(Exception{Code: code, Message: message})
}
//尝试
func Try(handler func()) *ExceptionStruct {
return &ExceptionStruct{
try: handler,
}
}
//捕获异常方法设置
func (ex *ExceptionStruct) Catch(catch func(Exception)) *ExceptionStruct {
ex.catch = catch
return ex
}
//最终都要执行的方法
func (ex *ExceptionStruct) Finally(finally func()) *ExceptionStruct {
ex.finally = finally
return ex
}
//这里总体执行
func (ex *ExceptionStruct) Exc() {
defer func() {
if e := recover(); nil != e {
exception := e.(Exception)
catch := ex.catch
if catch != nil {
catch(exception)
}
}
finally := ex.finally
if finally != nil {
finally()
}
}()
ex.try()
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/tym_hmm/go-helper.git
git@gitee.com:tym_hmm/go-helper.git
tym_hmm
go-helper
go-helper
v1.1.50

搜索帮助