Error code standardization

test-errcode
skiffer-git 2 years ago
parent e03a34cae4
commit b81513e9bb

@ -800,7 +800,7 @@ func FillPublicUserInfoByUserID(operationID, userID string, userInfo *open_im_sd
func SetErr(ctx context.Context, funcName string, err error, errCode *int32, errMsg *string, args ...interface{}) {
errInfo := constant.ToAPIErrWithErr(err)
*errCode = errInfo.ErrCode
*errMsg = errInfo.ErrMsg
*errMsg = errInfo.WrapErrMsg
trace_log.SetContextInfo(ctx, funcName, err, args)
}
@ -885,11 +885,11 @@ func CheckPermission(ctx context.Context, groupID string, userID string) error {
}
func (s *groupServer) GroupApplicationResponse(ctx context.Context, req *pbGroup.GroupApplicationResponseReq) (*pbGroup.GroupApplicationResponseResp, error) {
nCtx := trace_log.NewRpcCtx(ctx, utils.GetSelfFuncName(), req.OperationID)
trace_log.SetRpcReqInfo(nCtx, utils.GetSelfFuncName(), req.String())
defer trace_log.ShowLog(nCtx)
ctx = trace_log.NewRpcCtx(ctx, utils.GetSelfFuncName(), req.OperationID)
trace_log.SetRpcReqInfo(ctx, utils.GetSelfFuncName(), req.String())
defer trace_log.ShowLog(ctx)
resp := pbGroup.GroupApplicationResponseResp{CommonResp: &pbGroup.CommonResp{}}
if err := CheckPermission(nCtx, req.GroupID, req.OpUserID); err != nil {
if err := CheckPermission(ctx, req.GroupID, req.OpUserID); err != nil {
SetErrorForResp(err, &resp.CommonResp.ErrCode, &resp.CommonResp.ErrMsg)
return &resp, nil
}
@ -898,11 +898,11 @@ func (s *groupServer) GroupApplicationResponse(ctx context.Context, req *pbGroup
groupRequest.UserID = req.FromUserID
groupRequest.HandleUserID = req.OpUserID
groupRequest.HandledTime = time.Now()
if err := (&imdb.GroupRequest{}).Update(nCtx, []*imdb.GroupRequest{&groupRequest}); err != nil {
if err := (&imdb.GroupRequest{}).Update(ctx, []*imdb.GroupRequest{&groupRequest}); err != nil {
SetErrorForResp(err, &resp.CommonResp.ErrCode, &resp.CommonResp.ErrMsg)
return &resp, nil
}
groupInfo, err := rocksCache.GetGroupInfoFromCache(nCtx, req.GroupID)
groupInfo, err := rocksCache.GetGroupInfoFromCache(ctx, req.GroupID)
if err != nil {
SetErrorForResp(err, &resp.CommonResp.ErrCode, &resp.CommonResp.ErrMsg)
return &resp, nil
@ -913,7 +913,7 @@ func (s *groupServer) GroupApplicationResponse(ctx context.Context, req *pbGroup
SetErrorForResp(err, &resp.CommonResp.ErrCode, &resp.CommonResp.ErrMsg)
return &resp, nil
}
request, err := (&imdb.GroupRequest{}).Take(nCtx, req.GroupID, req.FromUserID)
request, err := (&imdb.GroupRequest{}).Take(ctx, req.GroupID, req.FromUserID)
if err != nil {
SetErrorForResp(err, &resp.CommonResp.ErrCode, &resp.CommonResp.ErrMsg)
return &resp, nil

@ -24,52 +24,53 @@ func (e ErrInfo) Code() int32 {
}
var (
ErrNone = ErrInfo{0, ""}
ErrArgs = ErrInfo{ArgsError, "ArgsError"}
ErrDatabase = ErrInfo{DatabaseError, "DatabaseError"}
ErrInternalServer = ErrInfo{ServerInternalError, "ServerInternalError"}
ErrNetwork = ErrInfo{NetworkError, "NetworkError"}
ErrNoPermission = ErrInfo{NoPermissionError, "NoPermissionError"}
ErrUserIDNotFound = ErrInfo{UserIDNotFoundError, "UserIDNotFoundError"}
ErrGroupIDNotFound = ErrInfo{GroupIDNotFoundError, "GroupIDNotFoundError"}
ErrRecordNotFound = ErrInfo{RecordNotFoundError, "RecordNotFoundError"}
ErrRelationshipAlready = ErrInfo{RelationshipAlreadyError, "RelationshipAlreadyError"}
ErrNotRelationshipYet = ErrInfo{NotRelationshipYetError, "NotRelationshipYetError"}
ErrOnlyOneOwner = ErrInfo{OnlyOneOwnerError, "OnlyOneOwnerError"}
ErrInGroupAlready = ErrInfo{InGroupAlreadyError, "InGroupAlreadyError"}
ErrNotInGroupYet = ErrInfo{NotInGroupYetError, "NotInGroupYetError"}
ErrDismissedAlready = ErrInfo{DismissedAlreadyError, "DismissedAlreadyError"}
ErrOwnerNotAllowedQuit = ErrInfo{OwnerNotAllowedQuitError, "OwnerNotAllowedQuitError"}
ErrRegisteredAlready = ErrInfo{RegisteredAlreadyError, "RegisteredAlreadyError"}
ErrDefaultOther = ErrInfo{DefaultOtherError, "DefaultOtherError"}
ErrData = ErrInfo{DataError, "DataError"}
ErrTokenExpired = ErrInfo{TokenExpiredError, "TokenExpiredError"}
ErrTokenInvalid = ErrInfo{TokenInvalidError, "TokenInvalidError"} //
ErrTokenMalformed = ErrInfo{TokenMalformedError, "TokenMalformedError"} //格式错误
ErrTokenNotValidYet = ErrInfo{TokenNotValidYetError, "TokenNotValidYetError"} //还未生效
ErrTokenUnknown = ErrInfo{TokenUnknownError, "TokenUnknownError"} //未知错误
ErrTokenKicked = ErrInfo{TokenKickedError, "TokenKickedError"}
ErrTokenNotExist = ErrInfo{TokenNotExistError, "TokenNotExistError"} //在redis中不存在
ErrTokenDifferentPlatformID = ErrInfo{TokenDifferentPlatformIDError, "TokenDifferentPlatformIDError"}
ErrTokenDifferentUserID = ErrInfo{TokenDifferentUserIDError, "TokenDifferentUserIDError"}
ErrNone = ErrInfo{0, "", ""}
ErrArgs = ErrInfo{ArgsError, "ArgsError", ""}
ErrDatabase = ErrInfo{DatabaseError, "DatabaseError", ""}
ErrInternalServer = ErrInfo{ServerInternalError, "ServerInternalError", ""}
ErrNetwork = ErrInfo{NetworkError, "NetworkError", ""}
ErrNoPermission = ErrInfo{NoPermissionError, "NoPermissionError", ""}
ErrUserIDNotFound = ErrInfo{UserIDNotFoundError, "UserIDNotFoundError", ""}
ErrGroupIDNotFound = ErrInfo{GroupIDNotFoundError, "GroupIDNotFoundError", ""}
ErrRecordNotFound = ErrInfo{RecordNotFoundError, "RecordNotFoundError", ""}
ErrRelationshipAlready = ErrInfo{RelationshipAlreadyError, "RelationshipAlreadyError", ""}
ErrNotRelationshipYet = ErrInfo{NotRelationshipYetError, "NotRelationshipYetError", ""}
ErrOnlyOneOwner = ErrInfo{OnlyOneOwnerError, "OnlyOneOwnerError", ""}
ErrInGroupAlready = ErrInfo{InGroupAlreadyError, "InGroupAlreadyError", ""}
ErrNotInGroupYet = ErrInfo{NotInGroupYetError, "NotInGroupYetError", ""}
ErrDismissedAlready = ErrInfo{DismissedAlreadyError, "DismissedAlreadyError", ""}
ErrOwnerNotAllowedQuit = ErrInfo{OwnerNotAllowedQuitError, "OwnerNotAllowedQuitError", ""}
ErrRegisteredAlready = ErrInfo{RegisteredAlreadyError, "RegisteredAlreadyError", ""}
ErrDefaultOther = ErrInfo{DefaultOtherError, "DefaultOtherError", ""}
ErrData = ErrInfo{DataError, "DataError", ""}
ErrTokenExpired = ErrInfo{TokenExpiredError, "TokenExpiredError", ""}
ErrTokenInvalid = ErrInfo{TokenInvalidError, "TokenInvalidError", ""} //
ErrTokenMalformed = ErrInfo{TokenMalformedError, "TokenMalformedError", ""} //格式错误
ErrTokenNotValidYet = ErrInfo{TokenNotValidYetError, "TokenNotValidYetError", ""} //还未生效
ErrTokenUnknown = ErrInfo{TokenUnknownError, "TokenUnknownError", ""} //未知错误
ErrTokenKicked = ErrInfo{TokenKickedError, "TokenKickedError", ""}
ErrTokenNotExist = ErrInfo{TokenNotExistError, "TokenNotExistError", ""} //在redis中不存在
ErrTokenDifferentPlatformID = ErrInfo{TokenDifferentPlatformIDError, "TokenDifferentPlatformIDError", ""}
ErrTokenDifferentUserID = ErrInfo{TokenDifferentUserIDError, "TokenDifferentUserIDError", ""}
)
//var (
// ErrGroupStatusDismissed = errors.New("group dismissed")
// ErrNoGroupOwner = errors.New("no group owner")
//)
func toDetail(err error, info ErrInfo) ErrInfo {
errInfo := info
errInfo.WrapErrMsg = err.Error()
return errInfo
}
func ToAPIErrWithErr(err error) ErrInfo {
switch {
case errors.Is(err, gorm.ErrRecordNotFound):
return ErrRecordNotFound
return toDetail(err, ErrRecordNotFound)
case errors.Is(err, ErrArgs):
return ErrArgs
return toDetail(err, ErrArgs)
case errors.Is(err, ErrDatabase):
return ErrDatabase
}

Loading…
Cancel
Save