Skip to content

Commit eced082

Browse files
authored
fix: misleading error when sending different user id for the same email (#144)
* fix: error handling approach while sending headers * test: add test for duplicate record * chore: update error check
1 parent 79ca78b commit eced082

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

internal/server/v1beta1/server.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ func (server *APIServer) validateUserInCtx(ctx context.Context) (string, error)
5656
if errors.Is(err, user.ErrNoUserInformation) {
5757
return "", status.Errorf(codes.InvalidArgument, err.Error())
5858
}
59+
if errors.As(err, &user.DuplicateRecordError{UUID: usr.UUID, Email: usr.Email}) {
60+
return "", status.Errorf(codes.AlreadyExists, err.Error())
61+
}
5962
return "", status.Errorf(codes.Internal, codes.Internal.String())
6063
}
6164
if userID == "" {

internal/store/postgres/user_repository.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (r *UserRepository) UpsertByEmail(ctx context.Context, ud *user.User) (stri
3232
RETURNING id
3333
`, um.UUID, um.Email, um.Provider).Scan(&userID); err != nil {
3434
err := checkPostgresError(err)
35-
if errors.Is(err, errDuplicateKey) {
35+
if errors.Is(err, sql.ErrNoRows) {
3636
return "", user.DuplicateRecordError{UUID: ud.UUID, Email: ud.Email}
3737
}
3838
return "", err

internal/store/postgres/user_repository_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,22 @@ func (r *UserRepositoryTestSuite) TestUpsertByEmail() {
204204
r.Empty(id)
205205
})
206206

207+
r.Run("return ErrDuplicateRecord if record already exist", func() {
208+
usr := &user.User{UUID: uuid.NewString(), Email: "[email protected]"}
209+
210+
err := r.insertEmail(usr.Email)
211+
r.NoError(err)
212+
213+
usr.UUID = uuid.NewString()
214+
id, err := r.repository.UpsertByEmail(r.ctx, usr)
215+
r.NoError(err)
216+
r.NotEmpty(id)
217+
218+
id, err = r.repository.UpsertByEmail(r.ctx, usr)
219+
r.ErrorIs(err, user.DuplicateRecordError{UUID: usr.UUID, Email: usr.Email})
220+
r.Empty(id)
221+
})
222+
207223
r.Run("new row is inserted with uuid and email if user not exist", func() {
208224
usr := &user.User{UUID: uuid.NewString(), Email: "[email protected]"}
209225
id, err := r.repository.UpsertByEmail(r.ctx, usr)

0 commit comments

Comments
 (0)