diff --git a/internal/selection/selection.handler.go b/internal/selection/selection.handler.go index 7130b57..4b6d87b 100644 --- a/internal/selection/selection.handler.go +++ b/internal/selection/selection.handler.go @@ -33,13 +33,13 @@ type handlerImpl struct { func (h *handlerImpl) CreateSelection(c router.Context) { body := &dto.CreateSelectionRequest{} if err := c.Bind(body); err != nil { - h.log.Named("selection hdr").Error("failed to bind request body", zap.Error(err)) + h.log.Named("CreateSelection").Error("Bind: failed to bind request body", zap.Error(err)) c.BadRequestError(err.Error()) return } if errorList := h.validate.Validate(body); errorList != nil { - h.log.Named("selection hdr").Error("validation error", zap.Strings("errorList", errorList)) + h.log.Named("CreateSelection").Error("Validate: ", zap.Strings("errorList", errorList)) c.BadRequestError(strings.Join(errorList, ", ")) return } @@ -51,6 +51,7 @@ func (h *handlerImpl) CreateSelection(c router.Context) { res, appErr := h.svc.CreateSelection(req) if appErr != nil { + h.log.Named("CreateSelection").Error("CreateSelection: ", zap.Error(appErr)) c.ResponseError(appErr) return } @@ -61,7 +62,7 @@ func (h *handlerImpl) CreateSelection(c router.Context) { func (h *handlerImpl) FindByGroupIdSelection(c router.Context) { groupdId := c.Param("id") if groupdId == "" { - h.log.Named("selection hdr").Error("url parameter 'id' not found", zap.String("id", groupdId)) + h.log.Named("FindByGroupIdSelection").Error("Param: id not found") c.BadRequestError("url parameter 'id' not found") return } @@ -71,13 +72,14 @@ func (h *handlerImpl) FindByGroupIdSelection(c router.Context) { } if errorList := h.validate.Validate(req); errorList != nil { - h.log.Named("selection hdr").Error("validation error", zap.Strings("errorList", errorList)) + h.log.Named("FindByGroupIdSelection").Error("Validate: ", zap.Strings("errorList", errorList)) c.BadRequestError(strings.Join(errorList, ", ")) return } res, appErr := h.svc.FindByGroupIdSelection(req) if appErr != nil { + h.log.Named("FindByGroupIdSelection").Error("FindByGroupIdSelection: ", zap.Error(appErr)) c.ResponseError(appErr) return } @@ -88,13 +90,13 @@ func (h *handlerImpl) FindByGroupIdSelection(c router.Context) { func (h *handlerImpl) UpdateSelection(c router.Context) { body := &dto.UpdateSelectionRequest{} if err := c.Bind(body); err != nil { - h.log.Named("selection hdr").Error("failed to bind request body", zap.Error(err)) + h.log.Named("UpdateSelection").Error("Bind: ", zap.Error(err)) c.BadRequestError(err.Error()) return } if errorList := h.validate.Validate(body); errorList != nil { - h.log.Named("selection hdr").Error("validation error", zap.Strings("errorList", errorList)) + h.log.Named("UpdateSelection").Error("Validate: ", zap.Strings("errorList", errorList)) c.BadRequestError(strings.Join(errorList, ", ")) return } @@ -105,6 +107,7 @@ func (h *handlerImpl) UpdateSelection(c router.Context) { res, appErr := h.svc.UpdateSelection(req) if appErr != nil { + h.log.Named("UpdateSelection").Error("UpdateSelection: ", zap.Error(appErr)) c.ResponseError(appErr) return } diff --git a/internal/selection/selection.service.go b/internal/selection/selection.service.go index 143e540..a049e81 100644 --- a/internal/selection/selection.service.go +++ b/internal/selection/selection.service.go @@ -41,14 +41,18 @@ func (s *serviceImpl) CreateSelection(req *dto.CreateSelectionRequest) (*dto.Cre if err != nil { st, ok := status.FromError(err) if !ok { + s.log.Named("CreateSelection").Error("FromeError: ", zap.Error(err)) return nil, apperror.InternalServer } switch st.Code() { case codes.InvalidArgument: + s.log.Named("CreateSelection").Error("Create: ", zap.Error(err)) return nil, apperror.BadRequestError("Invalid argument") case codes.Internal: + s.log.Named("CreateSelection").Error("Create: ", zap.Error(err)) return nil, apperror.InternalServerError(err.Error()) default: + s.log.Named("CreateSelection").Error("Create: ", zap.Error(err)) return nil, apperror.ServiceUnavailable } } @@ -68,14 +72,18 @@ func (s *serviceImpl) FindByGroupIdSelection(req *dto.FindByGroupIdSelectionRequ if err != nil { st, ok := status.FromError(err) if !ok { + s.log.Named("FindByGroupIdSelection").Error("FromeError: ", zap.Error(err)) return nil, apperror.InternalServer } switch st.Code() { case codes.InvalidArgument: + s.log.Named("FindByGroupIdSelection").Error("FindByGroupId: ", zap.Error(err)) return nil, apperror.BadRequestError("Invalid argument") case codes.Internal: + s.log.Named("FindByGroupIdSelection").Error("FindByGroupId: ", zap.Error(err)) return nil, apperror.InternalServerError(err.Error()) default: + s.log.Named("FindByGroupIdSelection").Error("FindByGroupId: ", zap.Error(err)) return nil, apperror.ServiceUnavailable } } @@ -95,14 +103,18 @@ func (s *serviceImpl) UpdateSelection(req *dto.UpdateSelectionRequest) (*dto.Upd if err != nil { st, ok := status.FromError(err) if !ok { + s.log.Named("UpdateSelection").Error("FromeError: ", zap.Error(err)) return nil, apperror.InternalServer } switch st.Code() { case codes.InvalidArgument: + s.log.Named("UpdateSelection").Error("Update: ", zap.Error(err)) return nil, apperror.BadRequestError("Invalid argument") case codes.Internal: + s.log.Named("UpdateSelection").Error("Update: ", zap.Error(err)) return nil, apperror.InternalServerError(err.Error()) default: + s.log.Named("UpdateSelection").Error("Update: ", zap.Error(err)) return nil, apperror.ServiceUnavailable } } diff --git a/internal/selection/test/selection.handler_test.go b/internal/selection/test/selection.handler_test.go index 1a57a64..532b84f 100644 --- a/internal/selection/test/selection.handler_test.go +++ b/internal/selection/test/selection.handler_test.go @@ -74,14 +74,12 @@ func (t *SelectionHandlerTest) TestCreateSelectionInvalidArgument() { controller := gomock.NewController(t.T()) - selectionSvc := selectionMock.NewMockService(controller) - validator := validatorMock.NewMockDtoValidator(controller) context := routerMock.NewMockContext(controller) context.EXPECT().Bind(t.CreateSelectionReq).Return(expectedErr) context.EXPECT().BadRequestError(expectedErr.Error()) - handler := selection.NewHandler(selectionSvc, validator, t.logger) + handler := selection.NewHandler(nil, nil, t.logger) handler.CreateSelection(context) } @@ -130,14 +128,12 @@ func (t *SelectionHandlerTest) TestFindByStudentIdSelectionInvalidArgument() { controller := gomock.NewController(t.T()) - selectionSvc := selectionMock.NewMockService(controller) - validator := validatorMock.NewMockDtoValidator(controller) context := routerMock.NewMockContext(controller) context.EXPECT().Param("id").Return("") context.EXPECT().BadRequestError(expectedErr.Error()) - handler := selection.NewHandler(selectionSvc, validator, t.logger) + handler := selection.NewHandler(nil, nil, t.logger) handler.FindByGroupIdSelection(context) } @@ -185,16 +181,30 @@ func (t *SelectionHandlerTest) TestUpdateSelectionInvalidArgument() { controller := gomock.NewController(t.T()) - selectionSvc := selectionMock.NewMockService(controller) - validator := validatorMock.NewMockDtoValidator(controller) context := routerMock.NewMockContext(controller) context.EXPECT().Bind(t.UpdateSelectionReq).Return(expectedErr) context.EXPECT().BadRequestError(expectedErr.Error()) - handler := selection.NewHandler(selectionSvc, validator, t.logger) + handler := selection.NewHandler(nil, nil, t.logger) handler.UpdateSelection(context) } func (t *SelectionHandlerTest) TestUpdateSelectionInternalError() { + expectedErr := apperror.InternalServerError("internal error") + + controller := gomock.NewController(t.T()) + + selectionSvc := selectionMock.NewMockService(controller) + validator := validatorMock.NewMockDtoValidator(controller) + context := routerMock.NewMockContext(controller) + + context.EXPECT().Bind(t.UpdateSelectionReq).Return(nil) + validator.EXPECT().Validate(t.UpdateSelectionReq).Return(nil) + + selectionSvc.EXPECT().UpdateSelection(t.UpdateSelectionReq).Return(nil, expectedErr) + context.EXPECT().ResponseError(expectedErr) + + handler := selection.NewHandler(selectionSvc, validator, t.logger) + handler.UpdateSelection(context) }