提交 dc0f9fb2 authored 作者: mooncake's avatar mooncake

修改err

上级 95326137
package errcode package errcode
// http system level error code, error code range 10000~20000 // http system level error code, error code range 10000~10000
var ( var (
Success = NewError(1, "OK") Success = NewError(1, "OK")
// 已下错误会返给前端
InvalidParams = NewError(100001, "Invalid Parameter") InvalidParams = NewError(100001, "Invalid Parameter")
Unauthorized = NewError(100002, "Unauthorized") Unauthorized = NewError(100002, "Unauthorized")
InternalServerError = NewError(100003, "Internal Server Error") InternalServerError = NewError(100003, "Internal Server Error")
...@@ -16,24 +18,22 @@ var ( ...@@ -16,24 +18,22 @@ var (
AccessDenied = NewError(100011, "Access Denied") AccessDenied = NewError(100011, "Access Denied")
MethodNotAllowed = NewError(100012, "Method Not Allowed") MethodNotAllowed = NewError(100012, "Method Not Allowed")
ServiceUnavailable = NewError(100013, "Service Unavailable") ServiceUnavailable = NewError(100013, "Service Unavailable")
Canceled = NewError(100014, "Canceled")
Unknown = NewError(100015, "Unknown")
PermissionDenied = NewError(100016, "Permission Denied")
ResourceExhausted = NewError(100017, "Resource Exhausted")
FailedPrecondition = NewError(100018, "Failed Precondition")
Aborted = NewError(100019, "Aborted")
OutOfRange = NewError(100020, "Out Of Range")
Unimplemented = NewError(100021, "Unimplemented")
DataLoss = NewError(100022, "Data Loss")
StatusBadGateway = NewError(100023, "Bad Gateway")
Conflict = NewError(100024, "Conflict")
TooEarly = NewError(100025, "Too Early")
Canceled = NewError(100014, "Canceled") // 已下错误不会返给前端
Unknown = NewError(100015, "Unknown") DBError = NewError(160001, "DB Error")
PermissionDenied = NewError(100016, "Permission Denied") CacheError = NewError(160002, "Cache Error")
ResourceExhausted = NewError(100017, "Resource Exhausted") NetError = NewError(160003, "Net Error")
FailedPrecondition = NewError(100018, "Failed Precondition") FileError = NewError(160004, "File Error")
Aborted = NewError(100019, "Aborted")
OutOfRange = NewError(100020, "Out Of Range")
Unimplemented = NewError(100021, "Unimplemented")
DataLoss = NewError(100022, "Data Loss")
StatusBadGateway = NewError(100023, "Bad Gateway")
DBError = NewError(100024, "DB Error")
NetError = NewError(100025, "Net Error")
FileError = NewError(100026, "File Error")
BizLogicError = NewError(100027, "Business Logic Error")
// Deprecated: use Conflict instead
Conflict = NewError(100409, "Conflict")
TooEarly = NewError(100425, "Too Early")
) )
package xcode package xcode
// Code 通用错误代码接口定义。
type Code interface { type Code interface {
// Code 错误码。
Code() int Code() int
// Message 错误码简短信息。
Message() string Message() string
// Detail 错误码详细信息。
Detail() any Detail() any
} }
// ================================================================================================================
// 公共错误码定义。
// 保留内部错误码: code < 1000。
// ================================================================================================================
var ( var (
CodeDefault = 0 CodeDefault = 0
CodeOK = 200 CodeOK = 200
......
...@@ -16,11 +16,27 @@ import ( ...@@ -16,11 +16,27 @@ import (
// } // }
// } // }
func NewFileError(text string) error {
e := FileError{}
e.stack = callers()
e.text = text
e.code = errcode.FileError.Code()
return &e
}
func NewFileErrorf(format string, args ...any) error {
e := FileError{}
e.stack = callers()
e.text = fmt.Sprintf(format, args...)
e.code = errcode.FileError.Code()
return &e
}
func NewNetError(text string) error { func NewNetError(text string) error {
e := NetError{} e := NetError{}
e.stack = callers() e.stack = callers()
e.text = text e.text = text
e.code = xcode.CodeDefault e.code = errcode.NetError.Code()
return &e return &e
} }
...@@ -28,7 +44,7 @@ func NewNetErrorf(format string, args ...any) error { ...@@ -28,7 +44,7 @@ func NewNetErrorf(format string, args ...any) error {
e := NetError{} e := NetError{}
e.stack = callers() e.stack = callers()
e.text = fmt.Sprintf(format, args...) e.text = fmt.Sprintf(format, args...)
e.code = xcode.CodeDefault e.code = errcode.NetError.Code()
return &e return &e
} }
...@@ -36,7 +52,7 @@ func NewDBError(text string) error { ...@@ -36,7 +52,7 @@ func NewDBError(text string) error {
e := DBError{} e := DBError{}
e.stack = callers() e.stack = callers()
e.text = text e.text = text
e.code = xcode.CodeDefault e.code = errcode.DBError.Code()
return &e return &e
} }
...@@ -44,7 +60,7 @@ func NewDBErrorf(format string, args ...any) error { ...@@ -44,7 +60,7 @@ func NewDBErrorf(format string, args ...any) error {
e := DBError{} e := DBError{}
e.stack = callers() e.stack = callers()
e.text = fmt.Sprintf(format, args...) e.text = fmt.Sprintf(format, args...)
e.code = xcode.CodeDefault e.code = errcode.DBError.Code()
return &e return &e
} }
...@@ -52,7 +68,7 @@ func NewCacheError(text string) error { ...@@ -52,7 +68,7 @@ func NewCacheError(text string) error {
e := CacheError{} e := CacheError{}
e.stack = callers() e.stack = callers()
e.text = text e.text = text
e.code = xcode.CodeDefault e.code = errcode.CacheError.Code()
return &e return &e
} }
...@@ -60,7 +76,7 @@ func NewCacheErrorf(format string, args ...any) error { ...@@ -60,7 +76,7 @@ func NewCacheErrorf(format string, args ...any) error {
e := CacheError{} e := CacheError{}
e.stack = callers() e.stack = callers()
e.text = fmt.Sprintf(format, args...) e.text = fmt.Sprintf(format, args...)
e.code = xcode.CodeDefault e.code = errcode.CacheError.Code()
return &e return &e
} }
...@@ -76,31 +92,7 @@ func NewParamErrorf(format string, args ...any) error { ...@@ -76,31 +92,7 @@ func NewParamErrorf(format string, args ...any) error {
e := ParamError{} e := ParamError{}
e.stack = callers() e.stack = callers()
e.text = fmt.Sprintf(format, args...) e.text = fmt.Sprintf(format, args...)
e.code = xcode.CodeDefault e.code = errcode.InvalidParams.Code()
return &e
}
func NewBizError(text string) error {
e := BizError{}
e.stack = callers()
e.text = text
e.code = xcode.CodeDefault
return &e
}
func NewBizErrorC(code int, text string) error {
e := BizError{}
e.stack = callers()
e.text = text
e.code = code
return &e
}
func NewBizErrorf(format string, args ...any) error {
e := BizError{}
e.stack = callers()
e.text = fmt.Sprintf(format, args...)
e.code = xcode.CodeDefault
return &e return &e
} }
...@@ -108,7 +100,7 @@ func NewRuntimeError(text string) error { ...@@ -108,7 +100,7 @@ func NewRuntimeError(text string) error {
e := RuntimeError{} e := RuntimeError{}
e.stack = callers() e.stack = callers()
e.text = text e.text = text
e.code = xcode.CodeDefault e.code = errcode.InternalServerError.Code()
return &e return &e
} }
...@@ -116,11 +108,10 @@ func NewRuntimeErrorf(format string, args ...any) error { ...@@ -116,11 +108,10 @@ func NewRuntimeErrorf(format string, args ...any) error {
e := RuntimeError{} e := RuntimeError{}
e.stack = callers() e.stack = callers()
e.text = fmt.Sprintf(format, args...) e.text = fmt.Sprintf(format, args...)
e.code = xcode.CodeDefault e.code = errcode.InternalServerError.Code()
return &e return &e
} }
// NewC add code
func NewC(code int, text string) error { func NewC(code int, text string) error {
e := BizError{} e := BizError{}
e.stack = callers() e.stack = callers()
...@@ -160,6 +151,7 @@ func Newf(format string, args ...any) error { ...@@ -160,6 +151,7 @@ func Newf(format string, args ...any) error {
code: xcode.CodeDefault, code: xcode.CodeDefault,
} }
} }
func Errorf(format string, args ...any) error { func Errorf(format string, args ...any) error {
return &Err{ return &Err{
stack: callers(), stack: callers(),
...@@ -167,6 +159,7 @@ func Errorf(format string, args ...any) error { ...@@ -167,6 +159,7 @@ func Errorf(format string, args ...any) error {
code: xcode.CodeDefault, code: xcode.CodeDefault,
} }
} }
func NewCf(code int, format string, args ...any) error { func NewCf(code int, format string, args ...any) error {
return &Err{ return &Err{
stack: callers(), stack: callers(),
......
...@@ -41,6 +41,10 @@ type NetError struct { ...@@ -41,6 +41,10 @@ type NetError struct {
Err Err
} }
type FileError struct {
Err
}
type DBError struct { type DBError struct {
Err Err
} }
......
package xerror_test
import (
"fmt"
"testing"
"gitlab.wanzhuangkj.com/tush/xpkg/pkg/xerrors/xerror"
)
func TestErr_Error(t *testing.T) {
dbe := xerror.NewDBError("db err")
if e, ok := dbe.(*xerror.DBError); ok {
fmt.Println("is DBError,", e.Error())
}
if e, ok := dbe.(*xerror.Err); ok {
fmt.Println("is Err,", e.Error())
}
}
func TestErr_WarpError(t *testing.T) {
e := xerror.NewC(-1, "db error")
e = xerror.Wrap(e, "add wrap")
if v, ok := e.(*xerror.Err); ok {
fmt.Println("is Err,", v.Error(), v.Code())
}
}
package ecode
import (
"errors"
)
var (
ErrCronJobNotFound = errors.New("未查询到任务")
)
...@@ -2,19 +2,18 @@ package service ...@@ -2,19 +2,18 @@ package service
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"gitlab.wanzhuangkj.com/tush/xpkg/xcommon" "gitlab.wanzhuangkj.com/tush/xpkg/xcommon"
"gitlab.wanzhuangkj.com/tush/xpkg/xcron/biz" "gitlab.wanzhuangkj.com/tush/xpkg/xcron/biz"
"gitlab.wanzhuangkj.com/tush/xpkg/xcron/dao" "gitlab.wanzhuangkj.com/tush/xpkg/xcron/dao"
"gitlab.wanzhuangkj.com/tush/xpkg/xcron/ecode"
"gitlab.wanzhuangkj.com/tush/xpkg/xcron/enums" "gitlab.wanzhuangkj.com/tush/xpkg/xcron/enums"
"gitlab.wanzhuangkj.com/tush/xpkg/xcron/models" "gitlab.wanzhuangkj.com/tush/xpkg/xcron/models"
"gitlab.wanzhuangkj.com/tush/xpkg/xcron/types" "gitlab.wanzhuangkj.com/tush/xpkg/xcron/types"
"gitlab.wanzhuangkj.com/tush/xpkg/xutils/xsf" "gitlab.wanzhuangkj.com/tush/xpkg/xutils/xsf"
"github.com/jinzhu/copier" "github.com/jinzhu/copier"
"gitlab.wanzhuangkj.com/tush/xpkg/pkg/xerrors/xerror"
"gitlab.wanzhuangkj.com/tush/xpkg/xutils/sliceutils" "gitlab.wanzhuangkj.com/tush/xpkg/xutils/sliceutils"
) )
...@@ -62,7 +61,7 @@ func (x *cronJobService) DeleteByID(ctx context.Context, id xsf.ID) error { ...@@ -62,7 +61,7 @@ func (x *cronJobService) DeleteByID(ctx context.Context, id xsf.ID) error {
return err return err
} }
if cronJob == nil { if cronJob == nil {
return xerror.NewBizError(fmt.Sprintf("未查询到%s[id:%d]", models.CronJobTool.TableName(), id)) return errors.New(fmt.Sprintf("未查询到%s[id:%d]", models.CronJobTool.TableName(), id))
} }
return dao.CronJobDao.DeleteByID(ctx, id) return dao.CronJobDao.DeleteByID(ctx, id)
} }
...@@ -75,7 +74,7 @@ func (x *cronJobService) DeleteByIDs(ctx context.Context, ids []xsf.ID) error { ...@@ -75,7 +74,7 @@ func (x *cronJobService) DeleteByIDs(ctx context.Context, ids []xsf.ID) error {
if err != nil { if err != nil {
return err return err
} }
err = sliceutils.CompareSlice(ids, cronJobs, ecode.ErrCronJobNotFound) err = sliceutils.CompareSlice(ids, cronJobs)
if err != nil { if err != nil {
return err return err
} }
...@@ -107,7 +106,7 @@ func (x *cronJobService) UpdateByID(ctx context.Context, req *types.CronJobUpdat ...@@ -107,7 +106,7 @@ func (x *cronJobService) UpdateByID(ctx context.Context, req *types.CronJobUpdat
return err return err
} }
if cronJob == nil { if cronJob == nil {
return xerror.NewBizError(fmt.Sprintf("未查询到%s[id:%d]", models.CronJobTool.TableName(), req.ID)) return errors.New(fmt.Sprintf("未查询到%s[id:%d]", models.CronJobTool.TableName(), req.ID))
} }
cronJobUpd := &models.CronJob{} cronJobUpd := &models.CronJob{}
_ = copier.Copy(cronJobUpd, req) _ = copier.Copy(cronJobUpd, req)
...@@ -122,7 +121,7 @@ func (x *cronJobService) UpdateByIDs(ctx context.Context, req *types.CronJobUpda ...@@ -122,7 +121,7 @@ func (x *cronJobService) UpdateByIDs(ctx context.Context, req *types.CronJobUpda
if err != nil { if err != nil {
return err return err
} }
err = sliceutils.CompareSlice(req.IDs, cronJobs, ecode.ErrCronJobNotFound) err = sliceutils.CompareSlice(req.IDs, cronJobs)
if err != nil { if err != nil {
return err return err
} }
......
package sliceutils package sliceutils
import ( import (
"errors"
"fmt" "fmt"
"strings" "strings"
"gitlab.wanzhuangkj.com/tush/xpkg/pkg/errcode"
"gitlab.wanzhuangkj.com/tush/xpkg/pkg/xtype" "gitlab.wanzhuangkj.com/tush/xpkg/pkg/xtype"
"gitlab.wanzhuangkj.com/tush/xpkg/xutils/setutils" "gitlab.wanzhuangkj.com/tush/xpkg/xutils/setutils"
"github.com/jinzhu/copier" "github.com/jinzhu/copier"
"github.com/spf13/cast" "github.com/spf13/cast"
"gitlab.wanzhuangkj.com/tush/xpkg/pkg/xerrors/xerror"
) )
// connector 连接符 // connector 连接符
...@@ -236,23 +235,17 @@ type ICode interface { ...@@ -236,23 +235,17 @@ type ICode interface {
Code() int Code() int
} }
func CompareSlice[ID xtype.Key, T IIDTable[ID], BizSlice ~[]*T](ids []ID, bizSlice BizSlice, errCode error) error { func CompareSlice[ID xtype.Key, T IIDTable[ID], BizSlice ~[]*T](ids []ID, bizSlice BizSlice) error {
if len(ids) == 0 { if len(ids) == 0 {
return nil return nil
} }
if len(bizSlice) < len(ids) { if len(bizSlice) < len(ids) {
if len(bizSlice) == 0 { if len(bizSlice) == 0 {
if v, ok := errCode.(*errcode.Error); ok { return errors.New("ids(result) less then ids(params)")
return xerror.NewC(v.Code(), v.Msg())
}
return xerror.NewBizError(errCode.Error())
} }
deltaIDs := SetDifference(ids, GetIDs[ID](bizSlice)) deltaIDs := SetDifference(ids, GetIDs(bizSlice))
if len(deltaIDs) > 0 { if len(deltaIDs) > 0 {
if v, ok := errCode.(*errcode.Error); ok { return fmt.Errorf("ids not match: %s", Join(deltaIDs, ","))
return xerror.NewC(v.Code(), fmt.Sprintf("%s[ids:%s]", v.Msg(), Join(deltaIDs, ",")))
}
return xerror.NewBizError(errCode.Error())
} }
} }
return nil return nil
...@@ -272,30 +265,6 @@ func ArrayUnique[T comparable](array []T) []T { ...@@ -272,30 +265,6 @@ func ArrayUnique[T comparable](array []T) []T {
return array[:idx] return array[:idx]
} }
//
//type IId[id xtype.Key] interface {
// GetID() id
//}
//
//func GetIds[id xtype.Key, T IId[id]](rs []*T) []id {
// if len(rs) == 0 {
// return nil
// }
// set := setutils.NewSet[id]()
// for _, r := range rs {
// set.Add((*r).GetID())
// }
// return set.Slice()
//}
//
//func SliceToIdMap[id xtype.Key, T IId[id]](rs []*T) map[id]*T {
// m := make(map[id]*T)
// for _, r := range rs {
// m[(*r).GetID()] = r
// }
// return m
//}
func ChunkSlice[T any](slice []T, chunkSize int) [][]T { func ChunkSlice[T any](slice []T, chunkSize int) [][]T {
var chunks [][]T var chunks [][]T
for i := 0; i < len(slice); i += chunkSize { for i := 0; i < len(slice); i += chunkSize {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论