1 Star 0 Fork 0

曾广宇/mxx-core-v2

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
constant.go 2.26 KB
一键复制 编辑 原始数据 按行查看 历史
曾广宇 提交于 2024-06-13 17:27 . 格式化日志输出
package mexception
import (
"fmt"
"reflect"
)
var (
ERROR = Exception{1, "ERROR", ""} // 系统异常
RunTimeException = Exception{2, "RunTimeException", ""} // 运行时异常
NotFoundException = Exception{3, "NotFoundException", ""} // 不存在异常
UnsupportedException = Exception{4, "UnsupportedException", ""} // 不支持
IllegalParameterException = Exception{5, "IllegalParameterException", ""} // 非法参数
EmptyException = Exception{6, "EmptyException", ""} // 空集合异常
NilException = Exception{7, "NilException", ""} // 控制正异常
DateTimeFormatException = Exception{8, "DateTimeFormatException", ""} // 日期格式化异常
JsonParseException = Exception{9, "JsonParseException", ""} // JSON解析异常
ParamsParseException = Exception{10, "ParamsParseException", ""} // 参数解析异常
ValidationException = Exception{11, "ValidationException", ""} // 参数校验异常
FileToMaxException = Exception{12, "FileToMaxException", ""} // 文件过大
SQLException = Exception{101, "SQLException", ""} // sql 执行异常
MongoException = Exception{102, "MongoException", ""} // mongodb 执行异常
RedisException = Exception{103, "RedisException", ""} // redis 执行异常
RabbitException = Exception{104, "RabbitException", ""} // rabbit 执行异常
)
type Exception struct {
Code int
Name string
Message string
}
func (ce Exception) Msg(msg string) error {
return Exception{Code: ce.Code, Name: ce.Name, Message: msg}
}
func (ce Exception) Error() string {
return fmt.Sprintf("%s[%v]: 【%s】", ce.Name, ce.Code, ce.Message)
}
func NewException(ex Exception, message string) error {
return Exception{ex.Code, ex.Name, message}
}
func NewError(err error) error {
return Exception{ERROR.Code, ERROR.Name, err.Error()}
}
func NewErrorEx(ex Exception, err error) error {
return Exception{ex.Code, ex.Name, err.Error()}
}
func IsMException(err any) bool {
return reflect.TypeOf(err).String() == "mexception.Exception"
}
func Message(err error) string {
if IsMException(err) {
return err.(Exception).Message
} else {
return err.Error()
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/dennis-mxx/mxx-core-v2.git
git@gitee.com:dennis-mxx/mxx-core-v2.git
dennis-mxx
mxx-core-v2
mxx-core-v2
v0.3.5

搜索帮助