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{}) { func SetErr(ctx context.Context, funcName string, err error, errCode *int32, errMsg *string, args ...interface{}) {
errInfo := constant.ToAPIErrWithErr(err) errInfo := constant.ToAPIErrWithErr(err)
*errCode = errInfo.ErrCode *errCode = errInfo.ErrCode
*errMsg = errInfo.ErrMsg *errMsg = errInfo.WrapErrMsg
trace_log.SetContextInfo(ctx, funcName, err, args) 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) { func (s *groupServer) GroupApplicationResponse(ctx context.Context, req *pbGroup.GroupApplicationResponseReq) (*pbGroup.GroupApplicationResponseResp, error) {
nCtx := trace_log.NewRpcCtx(ctx, utils.GetSelfFuncName(), req.OperationID) ctx = trace_log.NewRpcCtx(ctx, utils.GetSelfFuncName(), req.OperationID)
trace_log.SetRpcReqInfo(nCtx, utils.GetSelfFuncName(), req.String()) trace_log.SetRpcReqInfo(ctx, utils.GetSelfFuncName(), req.String())
defer trace_log.ShowLog(nCtx) defer trace_log.ShowLog(ctx)
resp := pbGroup.GroupApplicationResponseResp{CommonResp: &pbGroup.CommonResp{}} 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) SetErrorForResp(err, &resp.CommonResp.ErrCode, &resp.CommonResp.ErrMsg)
return &resp, nil return &resp, nil
} }
@ -898,11 +898,11 @@ func (s *groupServer) GroupApplicationResponse(ctx context.Context, req *pbGroup
groupRequest.UserID = req.FromUserID groupRequest.UserID = req.FromUserID
groupRequest.HandleUserID = req.OpUserID groupRequest.HandleUserID = req.OpUserID
groupRequest.HandledTime = time.Now() 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) SetErrorForResp(err, &resp.CommonResp.ErrCode, &resp.CommonResp.ErrMsg)
return &resp, nil return &resp, nil
} }
groupInfo, err := rocksCache.GetGroupInfoFromCache(nCtx, req.GroupID) groupInfo, err := rocksCache.GetGroupInfoFromCache(ctx, req.GroupID)
if err != nil { if err != nil {
SetErrorForResp(err, &resp.CommonResp.ErrCode, &resp.CommonResp.ErrMsg) SetErrorForResp(err, &resp.CommonResp.ErrCode, &resp.CommonResp.ErrMsg)
return &resp, nil return &resp, nil
@ -913,7 +913,7 @@ func (s *groupServer) GroupApplicationResponse(ctx context.Context, req *pbGroup
SetErrorForResp(err, &resp.CommonResp.ErrCode, &resp.CommonResp.ErrMsg) SetErrorForResp(err, &resp.CommonResp.ErrCode, &resp.CommonResp.ErrMsg)
return &resp, nil 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 { if err != nil {
SetErrorForResp(err, &resp.CommonResp.ErrCode, &resp.CommonResp.ErrMsg) SetErrorForResp(err, &resp.CommonResp.ErrCode, &resp.CommonResp.ErrMsg)
return &resp, nil return &resp, nil

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

Loading…
Cancel
Save