From 26bc4c3d7b67f79a9a4433da97e1af092122c13f Mon Sep 17 00:00:00 2001 From: SchwarzSail <119314702+SchwarzSail@users.noreply.github.com> Date: Thu, 12 Dec 2024 12:32:44 +0800 Subject: [PATCH] fix: add addressing error example in examroom module (#130) * address error example in examroom --- api/handler/api/class_room_service.go | 7 ++++--- api/model/api/login_data.go | 4 ++-- api/mw/get_header.go | 2 ++ api/rpc/classroom.go | 2 +- pkg/utils/rpc.go | 2 +- 5 files changed, 10 insertions(+), 7 deletions(-) diff --git a/api/handler/api/class_room_service.go b/api/handler/api/class_room_service.go index 55940b51..c6c5fa70 100644 --- a/api/handler/api/class_room_service.go +++ b/api/handler/api/class_room_service.go @@ -21,6 +21,8 @@ package api import ( "context" + "github.com/west2-online/fzuhelper-server/pkg/logger" + "github.com/cloudwego/hertz/pkg/app" "github.com/west2-online/fzuhelper-server/api/model/api" @@ -29,7 +31,6 @@ import ( "github.com/west2-online/fzuhelper-server/kitex_gen/classroom" "github.com/west2-online/fzuhelper-server/kitex_gen/model" "github.com/west2-online/fzuhelper-server/pkg/errno" - "github.com/west2-online/fzuhelper-server/pkg/logger" ) // GetEmptyClassrooms . @@ -64,8 +65,8 @@ func GetExamRoomInfo(ctx context.Context, c *app.RequestContext) { var err error loginData, err := api.GetLoginData(ctx) if err != nil { - logger.Errorf("api.GetExamRoomInfo: GetLoginData error %v", err) - pack.RespError(c, errno.ParamError.WithError(err)) + logger.Errorf("Failed to get header in the context: %v", err) // 不属于业务错误 + pack.RespError(c, errno.ParamMissingHeader.WithMessage("Failed to get header in the context")) return } var req api.ExamRoomInfoRequest diff --git a/api/model/api/login_data.go b/api/model/api/login_data.go index 6014f6ef..d1495857 100644 --- a/api/model/api/login_data.go +++ b/api/model/api/login_data.go @@ -18,9 +18,9 @@ package api import ( "context" - "errors" "github.com/west2-online/fzuhelper-server/kitex_gen/model" + "github.com/west2-online/fzuhelper-server/pkg/errno" ) var loginDataKey *model.LoginData @@ -28,7 +28,7 @@ var loginDataKey *model.LoginData func GetLoginData(ctx context.Context) (*model.LoginData, error) { user, ok := FromContext(ctx) if !ok { - return nil, errors.New("获取Header错误") + return nil, errno.ParamMissingHeader.WithMessage("Failed to get header in context") } return user, nil } diff --git a/api/mw/get_header.go b/api/mw/get_header.go index 0f5a28d9..860540a3 100644 --- a/api/mw/get_header.go +++ b/api/mw/get_header.go @@ -26,6 +26,7 @@ import ( "github.com/west2-online/fzuhelper-server/api/pack" "github.com/west2-online/fzuhelper-server/kitex_gen/model" "github.com/west2-online/fzuhelper-server/pkg/errno" + "github.com/west2-online/fzuhelper-server/pkg/logger" ) // GetHeaderParams 获取请求头的信息,处理 id 和 cookies 并附加到 Context 中 @@ -34,6 +35,7 @@ func GetHeaderParams() app.HandlerFunc { id := string(c.GetHeader("id")) temp := string(c.GetHeader("cookies")) if id == "" || len(temp) == 0 { + logger.Errorf("GetHeaderParams: id or cookies is empty") pack.RespError(c, errno.ParamMissingHeader) c.Abort() return diff --git a/api/rpc/classroom.go b/api/rpc/classroom.go index 0796c6b8..1ce9094f 100644 --- a/api/rpc/classroom.go +++ b/api/rpc/classroom.go @@ -51,7 +51,7 @@ func GetExamRoomInfoRPC(ctx context.Context, req *classroom.ExamRoomInfoRequest) resp, err := classroomClient.GetExamRoomInfo(ctx, req) if err != nil { logger.Errorf("GetExamRoomInfoRPC: RPC called failed: %v", err.Error()) - return nil, errno.InternalServiceError.WithMessage(err.Error()) + return nil, errno.InternalServiceError.WithMessage(err.Error()) // 不属于业务错误 } if err = utils.HandleBaseRespWithCookie(resp.Base); err != nil { return nil, err diff --git a/pkg/utils/rpc.go b/pkg/utils/rpc.go index ada1e98d..e6390833 100644 --- a/pkg/utils/rpc.go +++ b/pkg/utils/rpc.go @@ -26,7 +26,7 @@ func IsSuccess(baseResp *model.BaseResp) bool { return baseResp.Code == errno.SuccessCode } -// HandleBaseRespWithCookie 调用jwch库的接口的结果处理 +// HandleBaseRespWithCookie 调用jwch库的接口的结果处理, 将 resp.Base 中包含的错误转换成 errno 类型 func HandleBaseRespWithCookie(baseResp *model.BaseResp) error { if baseResp.Code == errno.BizJwchCookieExceptionCode { return errno.NewErrNo(baseResp.Code, baseResp.Msg)