You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
OneAuth/libs/oerr/oerr.go

204 lines
4.5 KiB
Go

3 years ago
package oerr
import (
"gorm.io/gorm"
"strconv"
)
// 错误描述
type Code uint
/*
510, 00000 etc.
0
## 1
- 1 : ,
- 2 :
- 3 :
- 4 :
- 5 :
- 6 :
- 7 : ()
## 2 2
## 3,4
## 5
- 0 :
- 1 :
- 2 : ,
- 3 : ,
- 4 : ,
- 5 :
- 6 :
- 7 :
- 8 :
- 9 :
*/
3 years ago
// Unknown error
3 years ago
const (
Unknown Code = 0
)
const (
3 years ago
// DBErr 2 数据库错误
3 years ago
// -1 系统错误
// -2 数据读写错误
DBErr Code = 20001
ResourceCreatedFailed Code = 22012
ResourceDuplicated Code = 22021
ResourceNotExist Code = 22031
)
const (
3 years ago
// LogicErr 3 系统内逻辑错误
LogicErr Code = 30000
AppNotJoin Code = 30001
3 years ago
)
const (
3 years ago
// NotLogin
3 years ago
// 4 权限类型错误
// 1: 登录权限
// 2: 资源操作权限
NotLogin Code = 41001
LoginExpired Code = 41011
PassError Code = 41021
DisableLogin Code = 41031
AccountNotExist Code = 41041
NoAuth Code = 42011
)
// 6 参数类型错误
/*
-1:
-2:
-3:
-4:
*/
const (
MethodNotSupport Code = 61111
MethodNotAllowed Code = 61121
ApiArgsError Code = 62001
ApiArgsMissing Code = 62011
TableArgsMissing Code = 62021
TableArgsErr Code = 62031
FuncArgsError Code = 63001
UrlPatternNotSupport Code = 63117
UrlDefinedDuplicate Code = 63127
UrlParamDuplicate Code = 63137
DataError Code = 64009
)
// 7 : 时序(控制)错误
/*
-1: 访
*/
const (
AccessErr Code = 71001
AccessTooFast Code = 71010
)
var codeMap = map[Code]string{
Unknown: "unknown error",
DBErr: "db error",
ResourceCreatedFailed: "resource created failed",
ResourceDuplicated: "resource duplicated",
ResourceNotExist: "Resource not exist",
MethodNotSupport: "this http method is not supported",
MethodNotAllowed: "this http method is not allowed",
ApiArgsError: "base args error",
ApiArgsMissing: "missing args",
TableArgsMissing: "missing data",
TableArgsErr: "invalid table data",
FuncArgsError: "func args error",
UrlPatternNotSupport: "this router's url pattern is not supported.",
UrlDefinedDuplicate: "this router's url has been defined",
UrlParamDuplicate: "this param defined in router's url duplicated",
DataError: "data error",
NotLogin: "not login",
LoginExpired: "login expired",
DisableLogin: "disabled to login",
PassError: "password/account error",
AccountNotExist: "account not exist",
NoAuth: "no auth to access",
AccessErr: "access error",
AccessTooFast: "access too fast",
3 years ago
LogicErr: "logic error",
AppNotJoin: "not join in app",
3 years ago
}
func (c Code) Error() string {
return strconv.Itoa(int(c)) + ":" + c.String()
}
func (c Code) String() string {
s, ok := codeMap[c]
if ok && len(s) > 0 {
return s
}
return codeMap[Unknown]
}
3 years ago
// Attach 附加错误详细原因
3 years ago
func (c Code) Attach(errs ...error) (e error) {
e = c
for _, err := range errs {
if err != nil {
e = &wrapErr{msg: e.Error() + "\n" + err.Error(), err: e}
}
}
return e
}
func (c Code) AttachStr(errs ...string) (e error) {
e = c
for _, m := range errs {
if m != "" {
e = &wrapErr{
msg: e.Error() + "\n" + m,
err: e,
}
}
}
return e
}
func OfType(errMsg string) Code {
s := ""
if gorm.ErrRecordNotFound.Error() == errMsg {
return ResourceNotExist
}
for _, v := range errMsg {
if v == ':' {
break
}
s += string(v)
}
c, _ := strconv.Atoi(s)
return Code(c)
}
type wrapErr struct {
msg string
err error
}
func (w *wrapErr) Error() string {
return w.msg
}
func (w *wrapErr) UnWrap() error {
return w.err
}