From 470ecaa1c7d4e7cd200afa0b15e6671ca2ed2fa5 Mon Sep 17 00:00:00 2001 From: JJnvn Date: Sun, 30 Jun 2024 17:01:05 +0700 Subject: [PATCH] Add error constant and adjust error case --- constant/checkin.constant.go | 8 ++++++ internal/checkin/checkin.service.go | 43 ++++++++++------------------- 2 files changed, 22 insertions(+), 29 deletions(-) create mode 100644 constant/checkin.constant.go diff --git a/constant/checkin.constant.go b/constant/checkin.constant.go new file mode 100644 index 0000000..7ea4358 --- /dev/null +++ b/constant/checkin.constant.go @@ -0,0 +1,8 @@ +package constant + +const InternalServerErrorMessage = "Internal error" +const DatabaseConnectionErrorMessage = "Database connection error" +const InvalidDataErrorMessage = "Invalid data" +const ArgumentEmptyErrorMessage = "Argument cannot be empty" +const RequestCancelledErrorMessage = "Request canceled by the client" +const AlreadyCheckinErrorMessage = "Already check in" diff --git a/internal/checkin/checkin.service.go b/internal/checkin/checkin.service.go index 78284e1..e339c78 100644 --- a/internal/checkin/checkin.service.go +++ b/internal/checkin/checkin.service.go @@ -4,6 +4,7 @@ import ( "context" "errors" + "github.com/isd-sgcu/rpkm67-checkin/constant" proto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/checkin/checkin/v1" "github.com/isd-sgcu/rpkm67-model/model" @@ -38,23 +39,23 @@ func (s *serviceImpl) Create(_ context.Context, req *proto.CreateCheckInRequest) err := s.repo.FindByUserId(req.UserId, &checkin_userIds) if err != nil { s.log.Named("Create").Error("Create: ", zap.Error(err)) - return nil, status.Error(codes.Internal, "internal error") + return nil, status.Error(codes.Internal, constant.InternalServerErrorMessage) } for _, v := range checkin_userIds { if v.Event == req.Event && v.UserID == req.UserId { - return nil, status.Error(codes.AlreadyExists, "already checked in") + return nil, status.Error(codes.AlreadyExists, constant.AlreadyCheckinErrorMessage) } } err = s.repo.Create(checkin) if err != nil { s.log.Named("Create").Error("Create: ", zap.Error(err)) if errors.Is(err, gorm.ErrInvalidDB) { - return nil, status.Error(codes.Internal, "database connection error") + return nil, status.Error(codes.Internal, constant.DatabaseConnectionErrorMessage) } if errors.Is(err, gorm.ErrInvalidData) { - return nil, status.Error(codes.InvalidArgument, "invalid data") + return nil, status.Error(codes.InvalidArgument, constant.InvalidDataErrorMessage) } - return nil, status.Error(codes.Internal, "internal error") + return nil, status.Error(codes.Internal, constant.InternalServerErrorMessage) } return &proto.CreateCheckInResponse{ @@ -65,7 +66,7 @@ func (s *serviceImpl) Create(_ context.Context, req *proto.CreateCheckInRequest) func (s *serviceImpl) FindByEmail(_ context.Context, req *proto.FindByEmailCheckInRequest) (*proto.FindByEmailCheckInResponse, error) { if req.Email == "" { s.log.Named("FindByUserEmail").Error("FindByUserEmail: invalid user ID") - return nil, status.Error(codes.InvalidArgument, "email cannot be empty") + return nil, status.Error(codes.InvalidArgument, constant.ArgumentEmptyErrorMessage) } var checkins []*model.CheckIn @@ -73,20 +74,12 @@ func (s *serviceImpl) FindByEmail(_ context.Context, req *proto.FindByEmailCheck if err != nil { s.log.Named("FindByEmail").Error("FindByEmail: ", zap.Error(err)) if errors.Is(err, context.Canceled) { - return nil, status.Error(codes.Canceled, "request canceled by the client") + return nil, status.Error(codes.Canceled, constant.RequestCancelledErrorMessage) } if errors.Is(err, gorm.ErrInvalidDB) { - return nil, status.Error(codes.Internal, "database connection error") + return nil, status.Error(codes.Internal, constant.DatabaseConnectionErrorMessage) } - if errors.Is(err, context.DeadlineExceeded) { - return nil, status.Error(codes.DeadlineExceeded, "request timed out") - } - return nil, status.Error(codes.Internal, "internal error") - } - - if len(checkins) == 0 { - s.log.Named("FindByUserEmail").Error("email not found") - return nil, status.Error(codes.NotFound, "email not found") + return nil, status.Error(codes.Internal, constant.InternalServerErrorMessage) } return &proto.FindByEmailCheckInResponse{ @@ -97,7 +90,7 @@ func (s *serviceImpl) FindByEmail(_ context.Context, req *proto.FindByEmailCheck func (s *serviceImpl) FindByUserId(_ context.Context, req *proto.FindByUserIdCheckInRequest) (*proto.FindByUserIdCheckInResponse, error) { if req.UserId == "" { s.log.Named("FindByUserId").Error("FindByUserId: invalid user ID") - return nil, status.Error(codes.InvalidArgument, "user ID cannot be empty") + return nil, status.Error(codes.InvalidArgument, constant.ArgumentEmptyErrorMessage) } var checkins []*model.CheckIn @@ -105,20 +98,12 @@ func (s *serviceImpl) FindByUserId(_ context.Context, req *proto.FindByUserIdChe if err != nil { s.log.Named("FindByUserId").Error("FindByUserId: ", zap.Error(err)) if errors.Is(err, context.Canceled) { - return nil, status.Error(codes.Canceled, "request canceled by the client") + return nil, status.Error(codes.Canceled, constant.RequestCancelledErrorMessage) } if errors.Is(err, gorm.ErrInvalidDB) { - return nil, status.Error(codes.Internal, "database connection error") + return nil, status.Error(codes.Internal, constant.DatabaseConnectionErrorMessage) } - if errors.Is(err, context.DeadlineExceeded) { - return nil, status.Error(codes.DeadlineExceeded, "request timed out") - } - return nil, status.Error(codes.Internal, "internal error") - } - - if len(checkins) == 0 { - s.log.Named("FindByUserId").Error("user not found") - return nil, status.Error(codes.NotFound, "user not found") + return nil, status.Error(codes.Internal, constant.InternalServerErrorMessage) } return &proto.FindByUserIdCheckInResponse{