Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ErrForeignKeyViolated implemented #122

Merged
merged 2 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
10 changes: 6 additions & 4 deletions error_translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ import (
"gorm.io/gorm"
)

var errCodes = map[string]uint16{
"uniqueConstraint": 1062,
var errCodes = map[uint16]error{
1062: gorm.ErrDuplicatedKey,
1452: gorm.ErrForeignKeyViolated,
}

func (dialector Dialector) Translate(err error) error {
if mysqlErr, ok := err.(*mysql.MySQLError); ok {
if mysqlErr.Number == errCodes["uniqueConstraint"] {
return gorm.ErrDuplicatedKey
if translatedErr, found := errCodes[mysqlErr.Number]; found {
return translatedErr
}
return mysqlErr
}

return err
Expand Down
58 changes: 58 additions & 0 deletions error_translator_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package mysql

import (
"errors"
"testing"

"gorm.io/gorm"

"github.com/go-sql-driver/mysql"
)

func TestDialector_Translate(t *testing.T) {
normalErr := errors.New("normal error")

type fields struct {
Config *Config
}
type args struct {
err error
}
tests := []struct {
name string
fields fields
args args
want error
}{
{
name: "it should translate error to ErrDuplicatedKey when the error number is 1062",
args: args{err: &mysql.MySQLError{Number: uint16(1062)}},
want: gorm.ErrDuplicatedKey,
},
{
name: "it should translate error to ErrForeignKeyViolated when the error number is 1452",
args: args{err: &mysql.MySQLError{Number: uint16(1452)}},
want: gorm.ErrForeignKeyViolated,
},
{
name: "it should not translate the error when the error number is not registered in translated error codes",
args: args{err: &mysql.MySQLError{Number: uint16(8888)}},
want: &mysql.MySQLError{Number: uint16(8888)},
},
{
name: "it should not translate the error when the error is not a mysql error",
args: args{err: normalErr},
want: normalErr,
},
}
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() got error = %v, want 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,5 +4,5 @@ go 1.14

require (
github.com/go-sql-driver/mysql v1.7.0
gorm.io/gorm v1.25.1
gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
gorm.io/gorm v1.25.1 h1:nsSALe5Pr+cM3V1qwwQ7rOkw+6UeLrX5O4v3llhHa64=
gorm.io/gorm v1.25.1/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=