优化提交kit/biz

Signed-off-by: lixxxww <941403820@qq.com>
This commit is contained in:
lixxxww
2024-01-23 07:38:19 +00:00
committed by Gitee
parent 76ed6737a2
commit d947142c40
2 changed files with 108 additions and 0 deletions

35
kit/biz/bizerror.go Normal file
View File

@@ -0,0 +1,35 @@
package biz
// 业务错误
type BizError struct {
code int16
err string
}
var (
Success *BizError = NewBizErrCode(200, "success")
BizErr *BizError = NewBizErrCode(400, "biz error")
ServerError *BizError = NewBizErrCode(500, "服务器异常,请联系管理员")
PermissionErr *BizError = NewBizErrCode(4001, "没有权限操作可能是TOKEN过期了请先登录")
CasbinErr *BizError = NewBizErrCode(403, "没有API接口访问权限请联系管理员")
)
// 错误消息
func (e *BizError) Error() string {
return e.err
}
// 错误码
func (e *BizError) Code() int16 {
return e.code
}
// 创建业务逻辑错误结构体,默认为业务逻辑错误
func NewBizErr(msg string) *BizError {
return &BizError{code: BizErr.code, err: msg}
}
// 创建业务逻辑错误结构体可设置指定错误code
func NewBizErrCode(code int16, msg string) *BizError {
return &BizError{code: code, err: msg}
}