Skip to content

Commit e558dbd

Browse files
author
Hadi T
committed
handled error for insertion
1 parent c737fc7 commit e558dbd

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

internal/clients/repository.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ import (
66
"encoding/json"
77
"errors"
88
"fmt"
9-
9+
"log"
1010
"oauth-password/pkg/database"
1111
"oauth-password/pkg/oauth"
12+
"strings"
1213
)
1314

1415
type Repository struct {
@@ -37,11 +38,17 @@ func (r *Repository) InsertSingle(
3738

3839
_, err = r.client.ExecContext(
3940
ctx,
40-
`INSERT INTO public.client_credential (id, data) VALUES (DEFAULT, $1)`,
41+
`INSERT INTO client_credential (id, data) VALUES (DEFAULT, $1)`,
4142
data,
4243
)
4344
if err != nil {
44-
return nil, err
45+
if strings.Contains(err.Error(), `duplicate key value violates unique constraint "client_credential_username_idx"`) {
46+
return nil, fmt.Errorf(`username already exists`)
47+
}
48+
49+
log.Println(clientCredential.Username, err)
50+
51+
return nil, errors.New("unexpected error during insertion of new client")
4552
}
4653

4754
return &Entity{

0 commit comments

Comments
 (0)