contracts/errno.go

143 lines
6.1 KiB
Go
Raw Normal View History

2023-04-12 16:56:55 +08:00
package contracts
2024-04-25 21:09:32 +08:00
import (
"errors"
2024-07-04 15:50:56 +08:00
"log"
2024-04-25 21:09:32 +08:00
"github.com/samber/lo"
)
2023-12-27 13:38:55 +08:00
2023-04-12 16:56:55 +08:00
// 定义错误码
type Errno struct {
Code int `json:"code"`
Msg string `json:"msg"`
}
func (err Errno) Error() string {
return err.Msg
}
// 定义错误
type Err struct {
2023-12-27 13:38:55 +08:00
Code int `json:"code"` // 错误码
Msg string `json:"msg"` // 展示给用户看的
Errord string `json:"-"` // 内部错误信息
alert bool `json:"-"` // 是否显示内部错误信息
2023-04-12 16:56:55 +08:00
}
2023-12-27 13:38:55 +08:00
func (err Err) Error() string {
2024-07-04 15:50:56 +08:00
defer log.Println(err.Code, err.Msg, err.Errord)
2023-12-27 13:38:55 +08:00
return lo.Ternary(err.alert && err.Errord != "", err.Errord, err.Msg)
2023-04-12 16:56:55 +08:00
}
2024-04-25 21:09:32 +08:00
var ErrFuncNotImplemented = errors.New("func not implemented")
2023-04-12 16:56:55 +08:00
// 错误码设计
// 第一位表示错误级别, 1 为系统错误, 2 为普通错误
// 第二三位表示服务模块代码
// 第四五位表示具体错误代码
var (
OK = &Errno{Code: 0, Msg: "操作成功!"}
UserRequestApproval = &Errno{Code: 1, Msg: "已提交审批申请"}
// 系统错误, 前缀为 100
2023-10-30 15:00:17 +08:00
InternalServerError = &Errno{Code: 10001, Msg: "内部服务器错误"}
ErrBind = &Errno{Code: 10002, Msg: "请求参数错误"}
ErrTokenSign = &Errno{Code: 10003, Msg: "签名 jwt 时发生错误"}
ErrEncrypt = &Errno{Code: 10004, Msg: "加密用户密码时发生错误"}
ErrNotFound = &Errno{Code: 10005, Msg: "Not Found"}
ErrFuncNotFound = &Errno{Code: 10006, Msg: "处理函数不存在"}
ErrResNotFound = &Errno{Code: 10007, Msg: "资源不存在"}
ErrResFieldNotFound = &Errno{Code: 10008, Msg: "资源字段不存在"}
ErrFileUpload = &Errno{Code: 10009, Msg: "文件上传错误"}
ErrFileNotFound = &Errno{Code: 10010, Msg: "文件不存在"}
ErrConfigureNotFound = &Errno{Code: 10011, Msg: "配置不存在"}
2024-02-01 15:54:40 +08:00
ErrConfigureInvalid = &Errno{Code: 10012, Msg: "配置参数无效"}
ErrParamRequired = &Errno{Code: 10013, Msg: "参数不存在"}
ErrParamInvalid = &Errno{Code: 10014, Msg: "参数无效"}
2023-04-12 16:56:55 +08:00
// service 错误
ErrServiceDecode = &Errno{Code: 10101, Msg: "解析 base62param 错误"}
ErrServiceInitInfoByList = &Errno{Code: 10102, Msg: "初始化列表错误"}
ErrServiceInitInitRelation = &Errno{Code: 10103, Msg: "初始化资源关联错误"}
ErrServiceInitInitField = &Errno{Code: 10103, Msg: "初始化资源关联错误"}
// 数据库错误, 前缀为 201
ErrDatabase = &Errno{Code: 20100, Msg: "数据库错误"}
ErrDBFill = &Errno{Code: 20101, Msg: "从数据库填充 struct 时发生错误"}
ErrDBQuery = &Errno{Code: 20102, Msg: "数据查询错误"}
2024-01-29 22:35:35 +08:00
ErrDBStore = &Errno{Code: 20103, Msg: "数据保存错误"}
ErrDBUpdate = &Errno{Code: 20104, Msg: "数据更新错误"}
ErrDBDelete = &Errno{Code: 20105, Msg: "数据删除错误"}
ErrDBTransaction = &Errno{Code: 20106, Msg: "数据库事务错误"}
2023-04-12 16:56:55 +08:00
// 认证错误, 前缀是 202
ErrTokenValidation = &Errno{Code: 20201, Msg: "验证失败"}
ErrTokenInvalid = &Errno{Code: 20202, Msg: "token 无效"}
2024-01-29 22:35:35 +08:00
ErrTokenExpired = &Errno{Code: 20203, Msg: "token 过期"}
ErrTokenNotFound = &Errno{Code: 20204, Msg: "token 不存在"}
2023-04-12 16:56:55 +08:00
// 用户错误, 前缀为 203
2023-12-27 10:39:55 +08:00
ErrUserNotLogin = &Errno{Code: 20300, Msg: "请先登录"}
2023-11-03 11:04:39 +08:00
ErrUserNotFound = &Errno{Code: 20301, Msg: "用户没找到"}
ErrUserPassword = &Errno{Code: 20302, Msg: "用户名或密码错误"}
ErrUserAuth = &Errno{Code: 20303, Msg: "无权限"}
ErrUserReadAuth = &Errno{Code: 20304, Msg: "无查询权限"}
ErrUserWriteAuth = &Errno{Code: 20305, Msg: "无写入权限"}
ErrUserIncomplete = &Errno{Code: 20306, Msg: "用户信息不完善"}
2023-11-03 13:50:30 +08:00
ErrUserWechatBound = &Errno{Code: 20307, Msg: "微信号绑定失败"}
2023-11-03 11:04:39 +08:00
ErrUserWechatPhoneBound = &Errno{Code: 20308, Msg: "该微信号已绑定手机"}
ErrUserWechatRegister = &Errno{Code: 20309, Msg: "通过微信注册失败"}
2023-04-12 16:56:55 +08:00
// 审批错误, 前缀是 204
2024-01-30 11:13:06 +08:00
ErrUserApprovalFlowNotFound = &Errno{Code: 20401, Msg: "审批节点不存在"}
2023-04-12 16:56:55 +08:00
ErrUserApprovalNextFlowNotFound = &Errno{Code: 20402, Msg: "下级审批节点不存在"}
2024-01-30 11:13:06 +08:00
ErrUserApprovalFlowDone = &Errno{Code: 20403, Msg: "该审批已结束"}
ErrUserApprovalFlowCancelled = &Errno{Code: 20404, Msg: "该审批已撤销"}
ErrUserApprovalMaxExecuteDeep = &Errno{Code: 20405, Msg: "该审批已超过自动扭转最大深度撤销"}
ErrUserApprovalExists = &Errno{Code: 20406, Msg: "该审批流已被占用"}
2023-04-12 16:56:55 +08:00
// 请求第三个接口
ErrApiRequest = &Errno{Code: 20501, Msg: "Api Request Error"}
ErrApiResponsed = &Errno{Code: 20502, Msg: "Api Responsed Error"}
2024-11-22 15:39:14 +08:00
ErrApiAuth = &Errno{Code: 20503, Msg: "Api Auth Error"}
2024-01-29 22:35:35 +08:00
// 验证码错误,206
2024-01-31 15:18:15 +08:00
ErrCaptcha = &Errno{Code: 20601, Msg: "验证码生成器不存在"}
ErrCaptchaGenerate = &Errno{Code: 20602, Msg: "验证码生成错误"}
ErrCaptchaInvalid = &Errno{Code: 20603, Msg: "验证码无效"}
ErrCaptchaExpired = &Errno{Code: 20604, Msg: "验证码已过期"}
ErrCaptchaSend = &Errno{Code: 20605, Msg: "验证码发送错误"}
ErrCaptchaInterval = &Errno{Code: 20606, Msg: "验证码获取失败, 请稍后再试"}
2024-02-01 15:54:40 +08:00
2024-11-22 15:39:14 +08:00
// Pkg 错误
ErrPkgUnknown = &Errno{Code: 20701, Msg: "pkg unknown"}
ErrPkgInvalid = &Errno{Code: 20702, Msg: "pkg invalid"}
ErrPkgPlatformInvalid = &Errno{Code: 20703, Msg: "pkg platform invalid"}
ErrPkgUpgrade = &Errno{Code: 20704, Msg: "pkg upgrade fail"}
2024-11-22 15:43:05 +08:00
ErrPkgRollback = &Errno{Code: 20705, Msg: "pkg version rollback fail"}
ErrPkgDownload = &Errno{Code: 20706, Msg: "pkg download fail"}
ErrPkgUpload = &Errno{Code: 20707, Msg: "pkg upload fail"}
ErrPkgBuild = &Errno{Code: 20708, Msg: "pkg build fail"}
2023-04-12 16:56:55 +08:00
)
// 使用 错误码 和 error 创建新的 错误
func NewErr(errno *Errno, err error) *Err {
return &Err{
2023-12-27 13:38:55 +08:00
alert: errno.Code == InternalServerError.Code,
Code: errno.Code,
Msg: errno.Error(),
Errord: err.Error(),
}
}
func NewMsgErr(errno *Errno, err error) *Err {
return &Err{
alert: true,
2023-04-12 16:56:55 +08:00
Code: errno.Code,
2023-08-30 14:55:01 +08:00
Msg: errno.Error(),
Errord: err.Error(),
2023-04-12 16:56:55 +08:00
}
}