Skip to content

Commit

Permalink
fix: user service error types
Browse files Browse the repository at this point in the history
  • Loading branch information
bookpanda committed Dec 30, 2023
1 parent 084b48f commit 30df31f
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/internal/service/user/user.service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package user

import (
"context"
"errors"

"github.com/isd-sgcu/johnjud-auth/src/internal/constant"
"github.com/isd-sgcu/johnjud-auth/src/internal/domain/model"
Expand All @@ -10,6 +11,7 @@ import (
proto "github.com/isd-sgcu/johnjud-go-proto/johnjud/auth/user/v1"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"gorm.io/gorm"
)

type serviceImpl struct {
Expand All @@ -27,7 +29,7 @@ func (s *serviceImpl) FindOne(_ context.Context, request *proto.FindOneUserReque

err := s.repo.FindById(request.Id, &raw)
if err != nil {
return nil, status.Error(codes.NotFound, "user not found")
return nil, status.Error(codes.Internal, constant.InternalServerErrorMessage)
}

return &proto.FindOneUserResponse{User: RawToDto(&raw)}, nil
Expand All @@ -48,7 +50,10 @@ func (s *serviceImpl) Update(_ context.Context, request *proto.UpdateUserRequest

err = s.repo.Update(request.Id, updateUser)
if err != nil {
return nil, status.Error(codes.NotFound, "user not found")
if errors.Is(err, gorm.ErrDuplicatedKey) {
return nil, status.Error(codes.AlreadyExists, constant.DuplicateEmailErrorMessage)
}
return nil, status.Error(codes.Internal, constant.InternalServerErrorMessage)
}

return &proto.UpdateUserResponse{User: RawToDto(updateUser)}, nil
Expand All @@ -57,7 +62,7 @@ func (s *serviceImpl) Update(_ context.Context, request *proto.UpdateUserRequest
func (s *serviceImpl) Delete(_ context.Context, request *proto.DeleteUserRequest) (*proto.DeleteUserResponse, error) {
err := s.repo.Delete(request.Id)
if err != nil {
return nil, status.Error(codes.NotFound, "something wrong when deleting user")
return nil, status.Error(codes.Internal, constant.InternalServerErrorMessage)
}

return &proto.DeleteUserResponse{Success: true}, nil
Expand Down

0 comments on commit 30df31f

Please sign in to comment.