first commit
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
package contracts
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
// 定义错误码
|
||||
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:"errord"` // 内部错误信息
|
||||
}
|
||||
|
||||
func (err *Err) Error() string {
|
||||
return fmt.Sprintf("Err - code: %d, message: %s, error: %s", err.Code, err.Msg, err.Errord)
|
||||
}
|
||||
|
||||
// 错误码设计
|
||||
// 第一位表示错误级别, 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: "文件不存在"}
|
||||
|
||||
// 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: 20102, Msg: "数据保存错误"}
|
||||
ErrDBUpdate = &Errno{Code: 20103, Msg: "数据更新错误"}
|
||||
ErrDBDelete = &Errno{Code: 20104, Msg: "数据删除错误"}
|
||||
ErrDBTransaction = &Errno{Code: 20105, Msg: "数据库事务错误"}
|
||||
|
||||
// 认证错误, 前缀是 202
|
||||
ErrTokenValidation = &Errno{Code: 20201, Msg: "验证失败"}
|
||||
ErrTokenInvalid = &Errno{Code: 20202, Msg: "token 无效"}
|
||||
ErrTokenExpired = &Errno{Code: 20202, Msg: "token 过期"}
|
||||
ErrTokenNotFound = &Errno{Code: 20202, Msg: "token 不存在"}
|
||||
|
||||
// 用户错误, 前缀为 203
|
||||
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: "无写入权限"}
|
||||
|
||||
// 审批错误, 前缀是 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"}
|
||||
)
|
||||
|
||||
// 使用 错误码 和 error 创建新的 错误
|
||||
func NewErr(errno *Errno, err error) *Err {
|
||||
stackBuf := make([]byte, 1024)
|
||||
n := runtime.Stack(stackBuf[:], false)
|
||||
|
||||
return &Err{
|
||||
Code: errno.Code,
|
||||
Msg: err.Error(),
|
||||
Errord: string(stackBuf[:n]),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user