Skip to content

Commit

Permalink
feat: foreign key violated error (#193)
Browse files Browse the repository at this point in the history
* feat: foreign key violated error

* ci: fixed ci workflow to be triggered for all pull requests

* test: fixed typo

---------

Co-authored-by: Saeid Saeidee <s.saeidee@sensysgatso.com>
  • Loading branch information
saeidee and Saeid Saeidee authored Jun 7, 2023
1 parent 64c0de1 commit 18bc84b
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 12 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ name: tests

on:
push:
branches:
- '**'
- '*'
pull_request:
branches:
- master

permissions:
contents: read
Expand Down
13 changes: 7 additions & 6 deletions error_translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
"github.com/jackc/pgx/v5/pgconn"
)

var errCodes = map[string]string{
"uniqueConstraint": "23505",
var errCodes = map[string]error{
"23505": gorm.ErrDuplicatedKey,
"23503": gorm.ErrForeignKeyViolated,
}

type ErrMessage struct {
Expand All @@ -22,8 +23,8 @@ type ErrMessage struct {
// Since currently gorm supporting both pgx and pg drivers, only checking for pgx PgError types is not enough for translating errors, so we have additional error json marshal fallback.
func (dialector Dialector) Translate(err error) error {
if pgErr, ok := err.(*pgconn.PgError); ok {
if pgErr.Code == errCodes["uniqueConstraint"] {
return gorm.ErrDuplicatedKey
if translatedErr, found := errCodes[pgErr.Code]; found {
return translatedErr
}
return err
}
Expand All @@ -39,8 +40,8 @@ func (dialector Dialector) Translate(err error) error {
return err
}

if errMsg.Code == errCodes["uniqueConstraint"] {
return gorm.ErrDuplicatedKey
if translatedErr, found := errCodes[errMsg.Code]; found {
return translatedErr
}
return err
}
44 changes: 44 additions & 0 deletions error_translator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package postgres

import (
"errors"
"github.com/jackc/pgx/v5/pgconn"
"gorm.io/gorm"
"testing"
)

func TestDialector_Translate(t *testing.T) {
type fields struct {
Config *Config
}
type args struct {
err error
}
tests := []struct {
name string
fields fields
args args
want error
}{
{
name: "it should return ErrDuplicatedKey error if the status code is 23505",
args: args{err: &pgconn.PgError{Code: "23505"}},
want: gorm.ErrDuplicatedKey,
},
{
name: "it should return ErrForeignKeyViolated error if the status code is 23503",
args: args{err: &pgconn.PgError{Code: "23503"}},
want: gorm.ErrForeignKeyViolated,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dialector := Dialector{
Config: tt.fields.Config,
}
if err := dialector.Translate(tt.args.err); !errors.Is(err, tt.want) {
t.Errorf("Translate() expected error = %v, got error %v", err, tt.want)
}
})
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.18

require (
github.com/jackc/pgx/v5 v5.3.1
gorm.io/gorm v1.25.0
gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gorm.io/gorm v1.25.0 h1:+KtYtb2roDz14EQe4bla8CbQlmb9dN3VejSai3lprfU=
gorm.io/gorm v1.25.0/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55 h1:sC1Xj4TYrLqg1n3AN10w871An7wJM0gzgcm8jkIkECQ=
gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=

0 comments on commit 18bc84b

Please sign in to comment.