114 lines
4.0 KiB
Go
114 lines
4.0 KiB
Go
package errcode
|
|
|
|
import "fmt"
|
|
|
|
var (
|
|
AuthNotFound = &BusinessErr{code: AuthErr, message: "账号不存在"}
|
|
AuthStatusFreeze = &BusinessErr{code: AuthErr, message: "账号冻结"}
|
|
AuthStatusDel = &BusinessErr{code: AuthErr, message: "身份验证失败"}
|
|
AuthStatusPwdFail = &BusinessErr{code: AuthErr, message: "密码错误"}
|
|
AuthTokenCreateFail = &BusinessErr{code: AuthErr, message: "token生成失败"}
|
|
AuthTokenDelFail = &BusinessErr{code: AuthErr, message: "删除token失败"}
|
|
AuthWxLoginFail = &BusinessErr{code: AuthErr, message: "微信登录失败,请稍后重试"}
|
|
AuthInfoFail = &BusinessErr{code: AuthErr, message: "登录异常,请重新登录"}
|
|
|
|
TokenNotFound = &BusinessErr{code: TokenErr, message: "缺少 Authorization Header"}
|
|
TokenFormatErr = &BusinessErr{code: TokenErr, message: "无效的token格式"}
|
|
TokenInfoNotFound = &BusinessErr{code: TokenErr, message: "未找到用户信息"}
|
|
TokenInvalid = &BusinessErr{code: TokenErr, message: "token过期"}
|
|
|
|
PlatsNotFound = &BusinessErr{code: NotFoundErr, message: "信息未找到"}
|
|
BadRequest = &BusinessErr{code: BadReqErr, message: "操作失败"}
|
|
|
|
ForbiddenError = &BusinessErr{code: ForbiddenErr, message: "权限不足"}
|
|
Success = &BusinessErr{code: 200, message: "成功"}
|
|
ParamError = &BusinessErr{code: ParamsErr, message: "参数错误"}
|
|
|
|
SystemError = &BusinessErr{code: 405, message: "系统错误"}
|
|
|
|
ClientNotFound = &BusinessErr{code: 406, message: "未找到client_id"}
|
|
SessionNotFound = &BusinessErr{code: 407, message: "未找到会话信息"}
|
|
UserNotFound = &BusinessErr{code: NotFoundErr, message: "不存在的用户"}
|
|
|
|
KeyNotFound = &BusinessErr{code: 409, message: "身份验证失败"}
|
|
SysNotFound = &BusinessErr{code: 410, message: "未找到系统信息"}
|
|
SysCodeNotFound = &BusinessErr{code: 411, message: "未找到系统编码"}
|
|
InvalidParam = &BusinessErr{code: InvalidParamCode, message: "无效参数"}
|
|
WorkflowError = &BusinessErr{code: 501, message: "工作流过程错误"}
|
|
ClientInfoNotFound = &BusinessErr{code: NotFoundErr, message: "用户信息未找到"}
|
|
)
|
|
|
|
const (
|
|
InvalidParamCode = 408
|
|
AuthErr = 403
|
|
TokenErr = 401
|
|
ParamsErr = 422
|
|
BadReqErr = 400
|
|
NotFoundErr = 404
|
|
ForbiddenErr = 403
|
|
BalanceNotEnoughCode = 402
|
|
)
|
|
|
|
type BusinessErr struct {
|
|
code int
|
|
message string
|
|
}
|
|
|
|
func (e *BusinessErr) Error() string {
|
|
return e.message
|
|
}
|
|
func (e *BusinessErr) Code() int {
|
|
return e.code
|
|
}
|
|
|
|
func NotFound(message string) *BusinessErr {
|
|
return &BusinessErr{code: NotFoundErr, message: PlatsNotFound.message + ":" + message}
|
|
}
|
|
|
|
func (e *BusinessErr) Is(target error) bool {
|
|
_, ok := target.(*BusinessErr)
|
|
return ok
|
|
}
|
|
|
|
// CustomErr 自定义错误
|
|
func NewBusinessErr(code int, message string) *BusinessErr {
|
|
return &BusinessErr{code: code, message: message}
|
|
}
|
|
|
|
func SysErrf(message string, arg ...any) *BusinessErr {
|
|
return &BusinessErr{code: SystemError.code, message: fmt.Sprintf(message, arg)}
|
|
}
|
|
|
|
func SysErr(message string) *BusinessErr {
|
|
return &BusinessErr{code: SystemError.code, message: message}
|
|
}
|
|
|
|
func ParamErrf(message string, arg ...any) *BusinessErr {
|
|
return &BusinessErr{code: ParamError.code, message: fmt.Sprintf(message, arg)}
|
|
}
|
|
|
|
func ParamErr(message string) *BusinessErr {
|
|
return &BusinessErr{code: ParamError.code, message: ParamError.message + ":" + message}
|
|
}
|
|
|
|
func SqlErr(err error) *BusinessErr {
|
|
|
|
return &BusinessErr{code: ParamError.code, message: "数据操作失败,请联系管理员处理:" + err.Error()}
|
|
}
|
|
|
|
func BadReq(message string) *BusinessErr {
|
|
return &BusinessErr{code: BadReqErr, message: BadRequest.message + ":" + message}
|
|
}
|
|
|
|
func Forbidden(message string) *BusinessErr {
|
|
return &BusinessErr{code: ForbiddenErr, message: ForbiddenError.message + ":" + message}
|
|
}
|
|
|
|
func (e *BusinessErr) Wrap(err error) *BusinessErr {
|
|
return NewBusinessErr(e.code, err.Error())
|
|
}
|
|
|
|
func BalanceNotEnoughErr(message string) *BusinessErr {
|
|
return NewBusinessErr(BalanceNotEnoughCode, message)
|
|
}
|