From 1d5e4a0f8a6903166c45c0e035c5e46e56e99d9e Mon Sep 17 00:00:00 2001 From: skiffer-git <44203734@qq.com> Date: Wed, 4 Jan 2023 11:06:28 +0800 Subject: [PATCH] Error code standardization --- internal/rpc/group/group.go | 33 ++++++++++++++++++++------------- pkg/common/constant/error.go | 3 +++ pkg/common/trace_log/ctx.go | 29 +++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 13 deletions(-) diff --git a/internal/rpc/group/group.go b/internal/rpc/group/group.go index f90d9b813..c62a7ac47 100644 --- a/internal/rpc/group/group.go +++ b/internal/rpc/group/group.go @@ -10,6 +10,7 @@ import ( "Open_IM/pkg/common/log" promePkg "Open_IM/pkg/common/prometheus" "Open_IM/pkg/common/token_verify" + "Open_IM/pkg/common/trace_log" cp "Open_IM/pkg/common/utils" open_im_sdk "Open_IM/pkg/proto/sdk_ws" "github.com/OpenIMSDK/getcdv3" @@ -780,7 +781,7 @@ func FillGroupInfoByGroupID(operationID, groupID string, groupInfo *open_im_sdk. } if group.Status == constant.GroupStatusDismissed { log.Debug(operationID, " group constant.GroupStatusDismissed ", group.GroupID) - return utils.Wrap(constant.ErrGroupStatusDismissed, "") + return utils.Wrap(constant.ErrDismissedAlready, "") } return utils.Wrap(cp.GroupDBCopyOpenIM(groupInfo, group), "") } @@ -795,19 +796,26 @@ func FillPublicUserInfoByUserID(operationID, userID string, userInfo *open_im_sd return nil } -func (s *groupServer) GetGroupApplicationList(_ context.Context, req *pbGroup.GetGroupApplicationListReq) (*pbGroup.GetGroupApplicationListResp, error) { - log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc args ", req.String()) +func SetErr(ctx context.Context, err error, errCode *int32, errMsg *string) { + errInfo := constant.ToAPIErrWithErr(err) + *errCode = errInfo.ErrCode + *errMsg = errInfo.ErrMsg + +} + +func (s *groupServer) GetGroupApplicationList(ctx context.Context, req *pbGroup.GetGroupApplicationListReq) (*pbGroup.GetGroupApplicationListResp, error) { + nCtx := trace_log.NewRpcCtx(ctx, utils.GetSelfFuncName(), req.OperationID) + trace_log.SetRpcReqInfo(nCtx, utils.GetSelfFuncName(), req.String()) + defer trace_log.ShowLog(nCtx) + resp := pbGroup.GetGroupApplicationListResp{} reply, err := imdb.GetRecvGroupApplicationList(req.FromUserID) if err != nil { - log.NewError(req.OperationID, "GetRecvGroupApplicationList failed ", err.Error(), req.FromUserID) - errInfo := constant.ToAPIErrWithErr(err) - resp.ErrCode = errInfo.ErrCode - resp.ErrMsg = errInfo.ErrMsg + SetErr(nCtx, err, &resp.ErrCode, &resp.ErrMsg) return &resp, nil } var errResult error - log.NewDebug(req.OperationID, "GetGroupApplicationList from db ", reply) + trace_log.SetContextInfo(nCtx, "GetRecvGroupApplicationList", nil, " FromUserID: ", req.FromUserID, "GroupApplicationList: ", reply) for _, v := range reply { node := open_im_sdk.GroupRequest{UserInfo: &open_im_sdk.PublicUserInfo{}, GroupInfo: &open_im_sdk.GroupInfo{}} @@ -818,7 +826,7 @@ func (s *groupServer) GetGroupApplicationList(_ context.Context, req *pbGroup.Ge } continue } - + trace_log.SetContextInfo(nCtx, "FillGroupInfoByGroupID ", nil, " groupID: ", v.GroupID, " groupInfo: ", node.GroupInfo) err = FillPublicUserInfoByUserID(req.OperationID, v.UserID, node.UserInfo) if err != nil { errResult = err @@ -828,11 +836,10 @@ func (s *groupServer) GetGroupApplicationList(_ context.Context, req *pbGroup.Ge resp.GroupRequestList = append(resp.GroupRequestList, &node) } if errResult != nil && len(resp.GroupRequestList) == 0 { - errInfo := constant.ToAPIErrWithErr(errResult) - resp.ErrCode = errInfo.ErrCode - resp.ErrMsg = errInfo.ErrMsg + SetErr(nCtx, err, &resp.ErrCode, &resp.ErrMsg) + return &resp, nil } - log.NewInfo(req.OperationID, utils.GetSelfFuncName(), " rpc return ", resp.String()) + trace_log.SetRpcRespInfo(nCtx, utils.GetSelfFuncName(), resp.String()) return &resp, nil } diff --git a/pkg/common/constant/error.go b/pkg/common/constant/error.go index 08c2641cd..edec51a97 100644 --- a/pkg/common/constant/error.go +++ b/pkg/common/constant/error.go @@ -71,6 +71,9 @@ func ToAPIErrWithErr(err error) ErrInfo { if errors.Is(errInfo, ErrArgs) { return ErrArgs } + if errors.Is(errInfo, ErrDatabase) { + return ErrDatabase + } } return ErrDefaultOther diff --git a/pkg/common/trace_log/ctx.go b/pkg/common/trace_log/ctx.go index 387ca0b55..4b2e21f32 100644 --- a/pkg/common/trace_log/ctx.go +++ b/pkg/common/trace_log/ctx.go @@ -17,6 +17,13 @@ func NewCtx(c *gin.Context, api string) context.Context { return context.WithValue(c, TraceLogKey, req) } +func NewRpcCtx(c context.Context, rpc string, operationID string) context.Context { + req := &ApiInfo{ApiName: rpc, Funcs: &[]FuncInfo{}} + ctx := context.WithValue(c, TraceLogKey, req) + SetOperationID(ctx, operationID) + return ctx +} + func SetOperationID(ctx context.Context, operationID string) { ctx.Value(TraceLogKey).(*ApiInfo).OperationID = operationID } @@ -69,6 +76,28 @@ func SetContextInfo(ctx context.Context, funcName string, err error, args ...int *t.Funcs = append(*t.Funcs, funcInfo) } +func SetRpcReqInfo(ctx context.Context, funcName string, req string) { + t := ctx.Value(TraceLogKey).(*ApiInfo) + var funcInfo FuncInfo + funcInfo.Args = make(map[string]interface{}) + var args []interface{} + args = append(args, " rpc req ", req) + argsHandle(args, funcInfo.Args) + funcInfo.FuncName = funcName + *t.Funcs = append(*t.Funcs, funcInfo) +} + +func SetRpcRespInfo(ctx context.Context, funcName string, resp string) { + t := ctx.Value(TraceLogKey).(*ApiInfo) + var funcInfo FuncInfo + funcInfo.Args = make(map[string]interface{}) + var args []interface{} + args = append(args, " rpc resp ", resp) + argsHandle(args, funcInfo.Args) + funcInfo.FuncName = funcName + *t.Funcs = append(*t.Funcs, funcInfo) +} + func SetSuccess(ctx context.Context, funcName string, data interface{}) { SetContextInfo(ctx, funcName, nil, "data", data) ctx.Value(TraceLogKey).(*ApiInfo).GinCtx.JSON(http.StatusOK, gin.H{"errCode": 0, "errMsg": "", "data": data})