package contracts import ( "errors" "log" "github.com/samber/lo" ) // 定义错误码 type Errno struct { Code int `json:"code"` Msg string `json:"msg"` } func (err Errno) Error() string { return err.Msg } // 定义错误 type Err struct { Code int `json:"code"` // 错误码 Msg string `json:"msg"` // 展示给用户看的 Errord string `json:"-"` // 内部错误信息 alert bool `json:"-"` // 是否显示内部错误信息 } func (err Err) Error() string { defer log.Println(err.Code, err.Msg, err.Errord) return lo.Ternary(err.alert && err.Errord != "", err.Errord, err.Msg) } var ErrFuncNotImplemented = errors.New("func not implemented") // 错误码设计 // 第一位表示错误级别, 1 为系统错误, 2 为普通错误 // 第二三位表示服务模块代码 // 第四五位表示具体错误代码 var ( OK = &Errno{Code: 0, Msg: "操作成功!"} UserRequestApproval = &Errno{Code: 1, Msg: "已提交审批申请"} // 系统错误, 前缀为 100 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: "配置不存在"} ErrConfigureInvalid = &Errno{Code: 10012, Msg: "配置参数无效"} ErrParamRequired = &Errno{Code: 10013, Msg: "参数不存在"} ErrParamInvalid = &Errno{Code: 10014, Msg: "参数无效"} // 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: "数据查询错误"} ErrDBStore = &Errno{Code: 20103, Msg: "数据保存错误"} ErrDBUpdate = &Errno{Code: 20104, Msg: "数据更新错误"} ErrDBDelete = &Errno{Code: 20105, Msg: "数据删除错误"} ErrDBTransaction = &Errno{Code: 20106, Msg: "数据库事务错误"} // 认证错误, 前缀是 202 ErrTokenValidation = &Errno{Code: 20201, Msg: "验证失败"} ErrTokenInvalid = &Errno{Code: 20202, Msg: "token 无效"} ErrTokenExpired = &Errno{Code: 20203, Msg: "token 过期"} ErrTokenNotFound = &Errno{Code: 20204, Msg: "token 不存在"} // 用户错误, 前缀为 203 ErrUserNotLogin = &Errno{Code: 20300, Msg: "请先登录"} 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: "用户信息不完善"} ErrUserWechatBound = &Errno{Code: 20307, Msg: "微信号绑定失败"} ErrUserWechatPhoneBound = &Errno{Code: 20308, Msg: "该微信号已绑定手机"} ErrUserWechatRegister = &Errno{Code: 20309, Msg: "通过微信注册失败"} // 审批错误, 前缀是 204 ErrUserApprovalFlowNotFound = &Errno{Code: 20401, Msg: "审批节点不存在"} ErrUserApprovalNextFlowNotFound = &Errno{Code: 20402, Msg: "下级审批节点不存在"} ErrUserApprovalFlowDone = &Errno{Code: 20403, Msg: "该审批已结束"} ErrUserApprovalFlowCancelled = &Errno{Code: 20404, Msg: "该审批已撤销"} ErrUserApprovalMaxExecuteDeep = &Errno{Code: 20405, Msg: "该审批已超过自动扭转最大深度撤销"} ErrUserApprovalExists = &Errno{Code: 20406, Msg: "该审批流已被占用"} // 请求第三个接口 ErrApiRequest = &Errno{Code: 20501, Msg: "Api Request Error"} ErrApiResponsed = &Errno{Code: 20502, Msg: "Api Responsed Error"} ErrApiAuth = &Errno{Code: 20503, Msg: "Api Auth Error"} // 验证码错误,206 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: "验证码获取失败, 请稍后再试"} // 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"} 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"} ) // 使用 错误码 和 error 创建新的 错误 func NewErr(errno *Errno, err error) *Err { return &Err{ 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, Code: errno.Code, Msg: errno.Error(), Errord: err.Error(), } }