Skip to content

Commit

Permalink
feat: use new svc error hanndling
Browse files Browse the repository at this point in the history
  • Loading branch information
bookpanda committed Jun 29, 2024
1 parent ea8dae4 commit 008cdb4
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 250 deletions.
41 changes: 3 additions & 38 deletions internal/checkin/checkin.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"github.com/isd-sgcu/rpkm67-gateway/internal/dto"
checkinProto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/checkin/checkin/v1"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

type Service interface {
Expand Down Expand Up @@ -41,18 +39,7 @@ func (s *serviceImpl) Create(req *dto.CreateCheckInRequest) (*dto.CreateCheckInR
})
if err != nil {
s.log.Named("Create").Error("Create: ", zap.Error(err))
st, ok := status.FromError(err)
if !ok {
return nil, apperror.InternalServer
}
switch st.Code() {
case codes.InvalidArgument:
return nil, apperror.BadRequest
case codes.Internal:
return nil, apperror.InternalServer
default:
return nil, apperror.ServiceUnavailable
}
return nil, apperror.HandleServiceError(err)
}

return &dto.CreateCheckInResponse{
Expand All @@ -74,18 +61,7 @@ func (s *serviceImpl) FindByEmail(req *dto.FindByEmailCheckInRequest) (*dto.Find
})
if err != nil {
s.log.Named("FindByEmail").Error("FindByEmail: ", zap.Error(err))
st, ok := status.FromError(err)
if !ok {
return nil, apperror.InternalServer
}
switch st.Code() {
case codes.InvalidArgument:
return nil, apperror.BadRequest
case codes.Internal:
return nil, apperror.InternalServer
default:
return nil, apperror.ServiceUnavailable
}
return nil, apperror.HandleServiceError(err)
}

return &dto.FindByEmailCheckInResponse{
Expand All @@ -102,18 +78,7 @@ func (s *serviceImpl) FindByUserID(req *dto.FindByUserIdCheckInRequest) (*dto.Fi
})
if err != nil {
s.log.Named("FindByUserID").Error("FindByUserID: ", zap.Error(err))
st, ok := status.FromError(err)
if !ok {
return nil, apperror.InternalServer
}
switch st.Code() {
case codes.InvalidArgument:
return nil, apperror.BadRequest
case codes.Internal:
return nil, apperror.InternalServer
default:
return nil, apperror.ServiceUnavailable
}
return nil, apperror.HandleServiceError(err)
}

return &dto.FindByUserIdCheckInResponse{
Expand Down
93 changes: 7 additions & 86 deletions internal/group/group.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"github.com/isd-sgcu/rpkm67-gateway/internal/dto"
groupProto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/backend/group/v1"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

type Service interface {
Expand Down Expand Up @@ -44,18 +42,7 @@ func (s *serviceImpl) DeleteMember(req *dto.DeleteMemberGroupRequest) (*dto.Dele
})
if err != nil {
s.log.Named("DeleteMember").Error("DeleteMember: ", zap.Error(err))
st, ok := status.FromError(err)
if !ok {
return nil, apperror.InternalServer
}
switch st.Code() {
case codes.InvalidArgument:
return nil, apperror.BadRequestError("")
case codes.Internal:
return nil, apperror.InternalServerError(err.Error())
default:
return nil, apperror.ServiceUnavailable
}
return nil, apperror.HandleServiceError(err)
}

return &dto.DeleteMemberGroupResponse{
Expand All @@ -72,18 +59,7 @@ func (s *serviceImpl) FindByToken(req *dto.FindByTokenGroupRequest) (*dto.FindBy
})
if err != nil {
s.log.Named("FindByToken").Error("FindByToken: ", zap.Error(err))
st, ok := status.FromError(err)
if !ok {
return nil, apperror.InternalServer
}
switch st.Code() {
case codes.InvalidArgument:
return nil, apperror.BadRequestError("Invalid argument")
case codes.Internal:
return nil, apperror.InternalServerError(err.Error())
default:
return nil, apperror.ServiceUnavailable
}
return nil, apperror.HandleServiceError(err)
}

return &dto.FindByTokenGroupResponse{
Expand All @@ -102,18 +78,7 @@ func (s *serviceImpl) FindOne(req *dto.FindOneGroupRequest) (*dto.FindOneGroupRe
})
if err != nil {
s.log.Named("FindOne").Error("FindOne: ", zap.Error(err))
st, ok := status.FromError(err)
if !ok {
return nil, apperror.InternalServer
}
switch st.Code() {
case codes.InvalidArgument:
return nil, apperror.BadRequestError("Invalid argument")
case codes.Internal:
return nil, apperror.InternalServerError(err.Error())
default:
return nil, apperror.ServiceUnavailable
}
return nil, apperror.HandleServiceError(err)
}

return &dto.FindOneGroupResponse{
Expand All @@ -132,18 +97,7 @@ func (s *serviceImpl) Join(req *dto.JoinGroupRequest) (*dto.JoinGroupResponse, *

if err != nil {
s.log.Named("Join").Error("Join: ", zap.Error(err))
st, ok := status.FromError(err)
if !ok {
return nil, apperror.InternalServer
}
switch st.Code() {
case codes.InvalidArgument:
return nil, apperror.BadRequestError("Invalid argument")
case codes.Internal:
return nil, apperror.InternalServerError(err.Error())
default:
return nil, apperror.ServiceUnavailable
}
return nil, apperror.HandleServiceError(err)
}

return &dto.JoinGroupResponse{
Expand All @@ -160,18 +114,7 @@ func (s *serviceImpl) Leave(req *dto.LeaveGroupRequest) (*dto.LeaveGroupResponse
})
if err != nil {
s.log.Named("Leave").Error("Leave: ", zap.Error(err))
st, ok := status.FromError(err)
if !ok {
return nil, apperror.InternalServer
}
switch st.Code() {
case codes.InvalidArgument:
return nil, apperror.BadRequestError("Invalid argument")
case codes.Internal:
return nil, apperror.InternalServerError(err.Error())
default:
return nil, apperror.ServiceUnavailable
}
return nil, apperror.HandleServiceError(err)
}

return &dto.LeaveGroupResponse{
Expand All @@ -189,18 +132,7 @@ func (s *serviceImpl) SelectBaan(req *dto.SelectBaanRequest) (*dto.SelectBaanRes
})
if err != nil {
s.log.Named("SelectBaan").Error("SelectBaan: ", zap.Error(err))
st, ok := status.FromError(err)
if !ok {
return nil, apperror.InternalServer
}
switch st.Code() {
case codes.InvalidArgument:
return nil, apperror.BadRequestError("Invalid argument")
case codes.Internal:
return nil, apperror.InternalServerError(err.Error())
default:
return nil, apperror.ServiceUnavailable
}
return nil, apperror.HandleServiceError(err)
}

return &dto.SelectBaanResponse{
Expand All @@ -218,18 +150,7 @@ func (s *serviceImpl) Update(req *dto.UpdateGroupRequest) (*dto.UpdateGroupRespo
})
if err != nil {
s.log.Named("Update").Error("Update: ", zap.Error(err))
st, ok := status.FromError(err)
if !ok {
return nil, apperror.InternalServer
}
switch st.Code() {
case codes.InvalidArgument:
return nil, apperror.BadRequestError("Invalid argument")
case codes.Internal:
return nil, apperror.InternalServerError(err.Error())
default:
return nil, apperror.ServiceUnavailable
}
return nil, apperror.HandleServiceError(err)
}

return &dto.UpdateGroupResponse{
Expand Down
28 changes: 2 additions & 26 deletions internal/pin/pin.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"github.com/isd-sgcu/rpkm67-gateway/internal/dto"
pinProto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/backend/pin/v1"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

type Service interface {
Expand All @@ -36,18 +34,7 @@ func (s *serviceImpl) FindAll(req *dto.FindAllPinRequest) (*dto.FindAllPinRespon
res, err := s.client.FindAll(ctx, &pinProto.FindAllPinRequest{})
if err != nil {
s.log.Named("FindAllBaan").Error("FindAllBaan: ", zap.Error(err))
st, ok := status.FromError(err)
if !ok {
return nil, apperror.InternalServer
}
switch st.Code() {
case codes.InvalidArgument:
return nil, apperror.BadRequestError("Invalid argument")
case codes.Internal:
return nil, apperror.InternalServerError(err.Error())
default:
return nil, apperror.ServiceUnavailable
}
return nil, apperror.HandleServiceError(err)
}

return &dto.FindAllPinResponse{
Expand All @@ -64,18 +51,7 @@ func (s *serviceImpl) ResetPin(req *dto.ResetPinRequest) (*dto.ResetPinResponse,
})
if err != nil {
s.log.Named("FindOneBaan").Error("FindOneBaan: ", zap.Error(err))
st, ok := status.FromError(err)
if !ok {
return nil, apperror.InternalServer
}
switch st.Code() {
case codes.NotFound:
return nil, apperror.NotFoundError("Pin not found")
case codes.Internal:
return nil, apperror.InternalServerError(err.Error())
default:
return nil, apperror.ServiceUnavailable
}
return nil, apperror.HandleServiceError(err)
}

return &dto.ResetPinResponse{
Expand Down
41 changes: 3 additions & 38 deletions internal/selection/selection.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"github.com/isd-sgcu/rpkm67-gateway/internal/dto"
selectionProto "github.com/isd-sgcu/rpkm67-go-proto/rpkm67/backend/selection/v1"
"go.uber.org/zap"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

type Service interface {
Expand Down Expand Up @@ -40,18 +38,7 @@ func (s *serviceImpl) CreateSelection(req *dto.CreateSelectionRequest) (*dto.Cre
})
if err != nil {
s.log.Named("CreateSelection").Error("Create: ", zap.Error(err))
st, ok := status.FromError(err)
if !ok {
return nil, apperror.InternalServer
}
switch st.Code() {
case codes.InvalidArgument:
return nil, apperror.BadRequestError("Invalid argument")
case codes.Internal:
return nil, apperror.InternalServerError(err.Error())
default:
return nil, apperror.ServiceUnavailable
}
return nil, apperror.HandleServiceError(err)
}

return &dto.CreateSelectionResponse{
Expand All @@ -68,18 +55,7 @@ func (s *serviceImpl) FindByGroupIdSelection(req *dto.FindByGroupIdSelectionRequ
})
if err != nil {
s.log.Named("FindByGroupIdSelection").Error("FindByGroupId: ", zap.Error(err))
st, ok := status.FromError(err)
if !ok {
return nil, apperror.InternalServer
}
switch st.Code() {
case codes.InvalidArgument:
return nil, apperror.BadRequestError("Invalid argument")
case codes.Internal:
return nil, apperror.InternalServerError(err.Error())
default:
return nil, apperror.ServiceUnavailable
}
return nil, apperror.HandleServiceError(err)
}

return &dto.FindByGroupIdSelectionResponse{
Expand All @@ -96,18 +72,7 @@ func (s *serviceImpl) DeleteSelection(req *dto.DeleteSelectionRequest) (*dto.Del
})
if err != nil {
s.log.Named("UpdateSelection").Error("Update: ", zap.Error(err))
st, ok := status.FromError(err)
if !ok {
return nil, apperror.InternalServer
}
switch st.Code() {
case codes.InvalidArgument:
return nil, apperror.BadRequestError("Invalid argument")
case codes.Internal:
return nil, apperror.InternalServerError(err.Error())
default:
return nil, apperror.ServiceUnavailable
}
return nil, apperror.HandleServiceError(err)
}

return &dto.DeleteSelectionResponse{
Expand Down
Loading

0 comments on commit 008cdb4

Please sign in to comment.