Error code standardization

This commit is contained in:
skiffer-git
2023-01-12 16:31:32 +08:00
parent dac62d62ef
commit 8286fdc4d0
6 changed files with 62 additions and 70 deletions
+21 -29
View File
@@ -16,17 +16,16 @@ type ErrInfo struct {
DetailErrMsg string
}
func (e ErrInfo) Error() string {
func (e *ErrInfo) Error() string {
return e.ErrMsg
}
func (e ErrInfo) Code() int32 {
func (e *ErrInfo) Code() int32 {
return e.ErrCode
}
var (
ErrNone = ErrInfo{0, "", ""}
ErrRpcConn = ErrInfo{GRPCConnIsNil, "grpc conn is nil", ""}
ErrArgs = ErrInfo{ArgsError, "ArgsError", ""}
ErrDatabase = ErrInfo{DatabaseError, "DatabaseError", ""}
ErrInternalServer = ErrInfo{ServerInternalError, "ServerInternalError", ""}
@@ -72,42 +71,35 @@ var (
)
func NewErrNetwork(err error) error {
newErrNetwork := ErrNetwork
newErrNetwork.DetailErrMsg = err.Error()
return ErrNetwork
return toDetail(err, &ErrNetwork)
}
func NewErrData(err error) error {
newErrData := ErrData
newErrData.DetailErrMsg = err.Error()
return ErrNetwork
return toDetail(err, &ErrData)
}
func toDetail(err error, info ErrInfo) ErrInfo {
errInfo := info
func toDetail(err error, info *ErrInfo) *ErrInfo {
errInfo := *info
errInfo.DetailErrMsg = err.Error()
return errInfo
return &errInfo
}
func ToAPIErrWithErr(err error) ErrInfo {
func ToAPIErrWithErr(err error) *ErrInfo {
errComm := errors.New("")
var marshalErr *json.MarshalerError
errInfo := &ErrInfo{}
switch {
case errors.Is(err, gorm.ErrRecordNotFound):
return toDetail(err, ErrRecordNotFound)
case errors.Is(err, ErrArgs):
return toDetail(err, ErrArgs)
case errors.Is(err, ErrDatabase):
return ErrDatabase
case errors.As(err, &errComm):
if errors.Is(err, gorm.ErrRecordNotFound) {
return toDetail(err, &ErrRecordNotFound)
}
return toDetail(err, &ErrData)
case errors.As(err, &marshalErr):
return toDetail(err, &ErrData)
case errors.As(err, &errInfo):
return toDetail(err, errInfo)
}
errTarget := errors.New("")
var mErr *json.MarshalerError
switch {
case errors.As(err, &mErr):
return ErrData
case errors.As(err, errTarget):
return ErrDatabase
}
return ErrDefaultOther
return toDetail(err, &ErrDefaultOther)
}
func SetErrorForResp(err error, commonResp *sdkws.CommonResp) {
+3 -3
View File
@@ -31,7 +31,7 @@ func Get(url string) (response []byte, err error) {
return body, nil
}
//application/json; charset=utf-8
// application/json; charset=utf-8
func Post(url string, data interface{}, timeOutSecond int) (content []byte, err error) {
jsonStr, err := json.Marshal(data)
if err != nil {
@@ -65,13 +65,13 @@ func CallBackPostReturn(url, callbackCommand string, input interface{}, output c
b, err := Post(url, input, timeOut)
if err != nil {
if failedContinue != nil && *failedContinue {
return constant.ErrCallbackContinue
return &constant.ErrCallbackContinue
}
return constant.NewErrNetwork(err)
}
if err = json.Unmarshal(b, output); err != nil {
if failedContinue != nil && *failedContinue {
return constant.ErrCallbackContinue
return &constant.ErrCallbackContinue
}
return constant.NewErrData(err)
}
+15 -15
View File
@@ -104,22 +104,22 @@ func GetClaimFromToken(tokensString string) (*Claims, error) {
if err != nil {
if ve, ok := err.(*jwt.ValidationError); ok {
if ve.Errors&jwt.ValidationErrorMalformed != 0 {
return nil, utils.Wrap(constant.ErrTokenMalformed, "")
return nil, utils.Wrap(&constant.ErrTokenMalformed, "")
} else if ve.Errors&jwt.ValidationErrorExpired != 0 {
return nil, utils.Wrap(constant.ErrTokenExpired, "")
return nil, utils.Wrap(&constant.ErrTokenExpired, "")
} else if ve.Errors&jwt.ValidationErrorNotValidYet != 0 {
return nil, utils.Wrap(constant.ErrTokenNotValidYet, "")
return nil, utils.Wrap(&constant.ErrTokenNotValidYet, "")
} else {
return nil, utils.Wrap(constant.ErrTokenUnknown, "")
return nil, utils.Wrap(&constant.ErrTokenUnknown, "")
}
} else {
return nil, utils.Wrap(constant.ErrTokenUnknown, "")
return nil, utils.Wrap(&constant.ErrTokenUnknown, "")
}
} else {
if claims, ok := token.Claims.(*Claims); ok && token.Valid {
return claims, nil
}
return nil, utils.Wrap(constant.ErrTokenUnknown, "")
return nil, utils.Wrap(&constant.ErrTokenUnknown, "")
}
}
@@ -162,7 +162,7 @@ func CheckAccessV2(ctx context.Context, OpUserID string, OwnerUserID string) (er
if OpUserID == OwnerUserID {
return nil
}
return utils.Wrap(constant.ErrIdentity, open_utils.GetSelfFuncName())
return utils.Wrap(&constant.ErrIdentity, open_utils.GetSelfFuncName())
}
func GetUserIDFromToken(token string, operationID string) (bool, string, string) {
@@ -211,26 +211,26 @@ func ParseToken(tokensString, operationID string) (claims *Claims, err error) {
m, err := commonDB.DB.GetTokenMapByUidPid(claims.UID, claims.Platform)
if err != nil {
log.NewError(operationID, "get token from redis err", err.Error(), claims.UID, claims.Platform)
return nil, utils.Wrap(constant.ErrTokenNotExist, "get token from redis err")
return nil, utils.Wrap(&constant.ErrTokenNotExist, "get token from redis err")
}
if m == nil {
log.NewError(operationID, "get token from redis err, not in redis ", "m is nil ", claims.UID, claims.Platform)
return nil, utils.Wrap(constant.ErrTokenNotExist, "get token from redis err")
return nil, utils.Wrap(&constant.ErrTokenNotExist, "get token from redis err")
}
if v, ok := m[tokensString]; ok {
switch v {
case constant.NormalToken:
log.NewDebug(operationID, "this is normal return", claims)
log.NewDebug(operationID, "this is normal return ", *claims)
return claims, nil
case constant.KickedToken:
log.Error(operationID, "this token has been kicked by other same terminal ", constant.ErrTokenKicked)
return nil, utils.Wrap(constant.ErrTokenKicked, "this token has been kicked by other same terminal ")
return nil, utils.Wrap(&constant.ErrTokenKicked, "this token has been kicked by other same terminal ")
default:
return nil, utils.Wrap(constant.ErrTokenUnknown, "")
return nil, utils.Wrap(&constant.ErrTokenUnknown, "")
}
}
log.NewError(operationID, "redis token map not find ", constant.ErrTokenNotExist, tokensString)
return nil, utils.Wrap(constant.ErrTokenNotExist, "redis token map not find")
return nil, utils.Wrap(&constant.ErrTokenNotExist, "redis token map not find")
}
//func MakeTheTokenInvalid(currentClaims *Claims, platformClass string) (bool, error) {
@@ -286,11 +286,11 @@ func WsVerifyToken(token, uid string, platformID string, operationID string) (bo
}
if claims.UID != uid {
errMsg := " uid is not same to token uid " + argMsg + " claims.UID: " + claims.UID
return false, utils.Wrap(constant.ErrTokenDifferentUserID, errMsg), errMsg
return false, utils.Wrap(&constant.ErrTokenDifferentUserID, errMsg), errMsg
}
if claims.Platform != constant.PlatformIDToName(utils.StringToInt(platformID)) {
errMsg := " platform is not same to token platform " + argMsg + " claims platformID: " + claims.Platform
return false, utils.Wrap(constant.ErrTokenDifferentPlatformID, errMsg), errMsg
return false, utils.Wrap(&constant.ErrTokenDifferentPlatformID, errMsg), errMsg
}
log.NewDebug(operationID, utils.GetSelfFuncName(), " check ok ", claims.UID, uid, claims.Platform)
return true, nil, ""