From bc04816c38c1f28116d8767a35fbeb9a3c78750a Mon Sep 17 00:00:00 2001 From: Rotem Tamir Date: Wed, 10 Mar 2021 14:07:00 +0200 Subject: [PATCH] entc/gen/template/dialect/sql: cleaning up isSQLConstraintError (#1319) * entc/gen/template/dialect/sql: cleaning up isSQLConstraintError in generated ent.go to use new sqlgraph IsConstraintError checks. Adding IsFK and IsUniqueness methods to the generated ent.ConstraintError (on SQL dialects) struct to allow users to glean more information about the specific constraint violation. Fixes #1310 * fix indentation in tmpl file regen files rm new methods from generated code * regen --- entc/gen/internal/bindata.go | 4 ++-- entc/gen/template/dialect/sql/errors.tmpl | 18 ++---------------- entc/integration/config/ent/ent.go | 19 ++----------------- entc/integration/customid/ent/ent.go | 19 ++----------------- entc/integration/edgefield/ent/ent.go | 19 ++----------------- entc/integration/ent/ent.go | 19 ++----------------- entc/integration/hooks/ent/ent.go | 19 ++----------------- entc/integration/idtype/ent/ent.go | 19 ++----------------- entc/integration/integration_test.go | 5 +++-- entc/integration/json/ent/ent.go | 19 ++----------------- entc/integration/migrate/entv1/ent.go | 19 ++----------------- entc/integration/migrate/entv2/ent.go | 19 ++----------------- entc/integration/multischema/ent/ent.go | 19 ++----------------- entc/integration/privacy/ent/ent.go | 19 ++----------------- entc/integration/template/ent/ent.go | 19 ++----------------- examples/edgeindex/ent/ent.go | 19 ++----------------- examples/entcpkg/ent/ent.go | 19 ++----------------- examples/m2m2types/ent/ent.go | 19 ++----------------- examples/m2mbidi/ent/ent.go | 19 ++----------------- examples/m2mrecur/ent/ent.go | 19 ++----------------- examples/o2m2types/ent/ent.go | 19 ++----------------- examples/o2mrecur/ent/ent.go | 19 ++----------------- examples/o2o2types/ent/ent.go | 19 ++----------------- examples/o2obidi/ent/ent.go | 19 ++----------------- examples/o2orecur/ent/ent.go | 19 ++----------------- examples/privacyadmin/ent/ent.go | 19 ++----------------- examples/privacytenant/ent/ent.go | 19 ++----------------- examples/start/ent/ent.go | 19 ++----------------- examples/traversal/ent/ent.go | 19 ++----------------- 29 files changed, 59 insertions(+), 462 deletions(-) diff --git a/entc/gen/internal/bindata.go b/entc/gen/internal/bindata.go index 24135276b..48928df8e 100644 --- a/entc/gen/internal/bindata.go +++ b/entc/gen/internal/bindata.go @@ -732,7 +732,7 @@ func templateDialectSqlEntqlTmpl() (*asset, error) { return a, nil } -var _templateDialectSqlErrorsTmpl = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x94\x53\x4d\x6b\xe3\x48\x10\x3d\xab\x7f\xc5\x5b\xc3\x2c\x52\xd0\x4a\x99\x1c\x16\x36\x83\x0f\x43\xd6\x0b\x01\x6f\x18\xc7\xc9\x61\x19\x86\xd0\x91\x4a\x72\xe3\x76\xb7\x5d\xdd\x72\x62\x84\xfe\xfb\xd2\xfa\x30\x26\xcb\x1e\x46\x27\x75\xbd\xaa\x57\x5f\xaf\xda\x36\xbf\x12\x77\x76\x7f\x62\x55\x6f\x3c\x6e\xae\x3f\xff\xf1\xdb\x9e\xc9\x91\xf1\xf8\x4b\x16\xf4\x6a\xed\x16\xf7\xa6\xc8\xf0\x55\x6b\xf4\x4e\x0e\x01\xe7\x23\x95\x99\x78\xda\x28\x07\x67\x1b\x2e\x08\x85\x2d\x09\xca\x41\xab\x82\x8c\xa3\x12\x8d\x29\x89\xe1\x37\x84\xaf\x7b\x59\x6c\x08\x37\xd9\xf5\x84\xa2\xb2\x8d\x29\x85\x32\x3d\xbe\xbc\xbf\x5b\x3c\xac\x17\xa8\x94\x26\x8c\x36\xb6\xd6\xa3\x54\x4c\x85\xb7\x7c\x82\xad\xe0\x2f\x92\x79\x26\xca\xc4\x55\xde\x75\x42\x84\x1e\x50\x34\xce\xdb\x1d\x88\xd9\xb2\x83\x34\xe5\xf4\xbb\x91\xa6\xd4\xc4\x0e\x95\x65\xb8\x83\x46\xa9\xa4\xa6\xc2\x3b\xf4\xd1\x6d\x8b\x92\x2a\x65\x08\xb3\x11\xc8\xdd\x41\xe7\x43\xf0\x0c\x5d\x27\xaa\xc6\x14\x50\x6e\xbd\x5a\xde\x59\xe3\x3c\x4b\x65\xfc\x22\xc0\x31\x31\x0f\x59\x12\xc4\x57\x1f\xc0\x14\xaf\xd6\xea\x04\xad\x88\x8e\x92\x11\x8b\x28\xda\xb9\x1a\xf3\x10\x90\x0d\xe1\x89\x88\xa2\x3c\x1f\x18\x42\x75\x3b\xe9\xb1\x27\x9e\x0a\xcc\x44\x14\x8d\x3d\xcc\xf1\x3d\xcb\xb2\x1f\xce\xb3\x32\x75\x2b\xa2\x28\x9a\xf5\x14\xf8\x7c\xfd\xfb\xcd\x2c\x8d\xce\x5f\x9e\xe3\xef\xd3\x7a\xb5\xec\x81\x91\x39\x5e\x3c\xbe\xfc\xf9\xfc\xed\x65\xf1\xf0\xf4\xf8\x4f\x92\xf5\xd1\xcf\x0f\xf7\xab\xe7\x05\x8a\x73\xcd\xa8\xa4\xd2\x54\x9e\xb9\xf2\x1c\xeb\xd5\x52\x79\x1a\xfc\xcb\x66\xaf\x55\x21\x3d\x61\x4b\x27\x1c\xa5\x6e\x08\x47\x65\xb5\xf4\xe4\xd0\x18\x75\x68\xe8\x82\x6c\x96\x86\xf8\x6f\xd6\xf9\x9a\x69\xbd\x5a\x06\x8e\x4e\x44\x89\x88\x54\x85\x97\x14\x76\x8b\xdb\x61\x10\xf1\x95\x3b\xe8\x9a\xe5\x7e\x93\x7d\x98\x5f\xf2\x25\xb8\x85\x5e\x99\x7c\xc3\x06\xbf\x7e\x70\x68\x77\xae\x4e\x03\x49\x97\xc2\x73\x43\x22\xa4\x08\x3b\x56\x81\x9c\xa5\xa9\x69\x92\x40\x60\x51\x15\x86\xf1\xb9\x90\xc9\x4b\x65\x5c\x3c\x31\x58\x76\xdf\xd5\x8f\x7e\x57\x3f\x91\x2e\xe4\xeb\xc4\xe4\x6f\x94\x4e\x51\x49\xed\x48\x74\x42\xe4\x39\xd8\x6a\xfd\x2a\x8b\x2d\x0a\xa9\xb5\x83\x7f\xcf\x1e\x27\x4b\x50\xe7\x1b\xcb\xbd\xeb\x85\x5e\xab\x23\x99\x71\x57\x6f\xca\x6f\x46\xf5\x8f\xbe\x83\x5d\x55\x18\xaf\x32\x1b\xf4\x38\xe1\xb1\x7f\x3f\xeb\xe5\xe9\xbd\x2f\x70\x92\xe4\x10\xd9\xf6\x43\xe7\x60\xbf\x9d\x5f\x56\x11\x27\x5f\x06\xf3\x2f\xf3\x50\x7c\xdf\x7c\x78\xce\x51\xed\xfc\xa0\xd0\x2a\x9e\x7d\x7a\xbb\xc5\xa7\xe3\xac\x27\x4e\x7b\xff\xa4\xef\x5a\x55\x83\x65\x58\xe5\xff\x5d\xc7\x7f\x96\x48\xcc\x97\x43\x0b\xcf\xfe\x04\xc9\x94\xe1\xd4\xfe\x0d\x00\x00\xff\xff\x87\x44\x71\x29\x8e\x04\x00\x00") +var _templateDialectSqlErrorsTmpl = []byte("\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\xff\x74\x52\x5d\x6b\xdb\x30\x14\x7d\xae\x7e\xc5\x59\xa0\x23\x2e\x99\xdd\xf5\x6d\x2d\x7d\x28\x21\x83\x40\x19\x6c\xed\x1f\x70\xa4\xab\x58\x44\x95\xd2\x2b\x39\x49\x31\xfe\xef\x43\x52\x0c\xa1\x6c\x6f\xd6\x39\xe7\x7e\x9c\x73\x3d\x0c\xcd\x8d\x58\xfa\xfd\x07\x9b\x6d\x17\x71\x77\xfb\xfd\xc7\xb7\x3d\x53\x20\x17\xf1\xb3\x95\xb4\xf1\x7e\x87\xb5\x93\x35\x9e\xac\x45\x16\x05\x24\x9e\x0f\xa4\x6a\xf1\xda\x99\x80\xe0\x7b\x96\x04\xe9\x15\xc1\x04\x58\x23\xc9\x05\x52\xe8\x9d\x22\x46\xec\x08\x4f\xfb\x56\x76\x84\xbb\xfa\x76\x62\xa1\x7d\xef\x94\x30\x2e\xf3\xcf\xeb\xe5\xea\xd7\xcb\x0a\xda\x58\xc2\x19\x63\xef\x23\x94\x61\x92\xd1\xf3\x07\xbc\x46\xbc\x18\x16\x99\xa8\x16\x37\xcd\x38\x0a\x91\x3c\x40\xf6\x21\xfa\x37\x10\xb3\xe7\x80\xd6\xa9\xe9\xb3\x6b\x9d\xb2\xc4\x01\xda\x33\xc2\xbb\x85\x32\xad\x25\x19\x03\x72\xf5\x30\x40\x91\x36\x8e\x30\x3b\x13\x4d\x78\xb7\x4d\x29\x9e\x61\x1c\x85\xee\x9d\x84\x09\x2f\xbf\x9f\x97\xde\x85\xc8\xad\x71\x71\x95\xe8\x39\x31\x97\x29\x15\xe6\x37\x9f\xc8\x05\x36\xde\xdb\x0a\x83\xb8\x32\x3a\xcd\xdd\x72\xbb\xef\xea\x75\xf8\x47\x93\xac\xba\x62\x8a\x3d\x3b\x7c\xfd\x24\x18\x88\xb9\x2e\xd2\x6a\x91\xc6\x8d\x0b\x44\xee\x49\x5c\x8d\x62\xaa\x71\xc6\x2e\xa0\x5b\x1b\x48\x8c\x42\x34\x0d\xd8\x5b\xbb\x69\xe5\x0e\xb2\xb5\x36\x20\x9e\xea\x3f\x13\x92\xa2\x39\x72\xbb\x0f\x39\xe5\xad\x39\x90\x2b\x26\x70\x34\xb1\x3b\x47\x7f\xd6\x16\xdc\x68\x9c\x7f\x89\xba\x84\x31\xf1\xf3\x78\x9a\xd2\xac\x5f\x4f\x79\xb9\x29\x8f\x52\x59\xcc\x73\xc2\xef\x1f\x2f\xb7\x98\x57\x0f\x05\xfe\xf2\x98\x96\xcf\xfe\xd3\xf3\x11\xfa\x2d\x16\xb7\x7a\x3e\xbb\x3e\xde\xe3\xfa\x30\xcb\x8d\x17\x59\x5f\x65\xd7\x46\x17\xc4\xef\x52\xdb\xff\x9d\xa6\x7a\x48\x82\x8b\x64\x89\xf9\x32\xb4\xf4\xcc\xf7\x27\xa7\xd2\x9d\xff\x06\x00\x00\xff\xff\xb1\x83\xfa\x00\x0b\x03\x00\x00") func templateDialectSqlErrorsTmplBytes() ([]byte, error) { return bindataRead( @@ -747,7 +747,7 @@ func templateDialectSqlErrorsTmpl() (*asset, error) { return nil, err } - info := bindataFileInfo{name: "template/dialect/sql/errors.tmpl", size: 1166, mode: os.FileMode(420), modTime: time.Unix(1, 0)} + info := bindataFileInfo{name: "template/dialect/sql/errors.tmpl", size: 779, mode: os.FileMode(420), modTime: time.Unix(1, 0)} a := &asset{bytes: bytes, info: info} return a, nil } diff --git a/entc/gen/template/dialect/sql/errors.tmpl b/entc/gen/template/dialect/sql/errors.tmpl index 6e44250c4..b8f6c5f03 100644 --- a/entc/gen/template/dialect/sql/errors.tmpl +++ b/entc/gen/template/dialect/sql/errors.tmpl @@ -7,22 +7,8 @@ in the LICENSE file in the root directory of this source tree. {{/* custom errors and errors handlers for sql dialects */}} {{ define "dialect/sql/errors" }} func isSQLConstraintError(err error) (*ConstraintError, bool) { - var ( - msg = err.Error() - // error format per dialect. - errors = [...]string{ - "Error 1062", // MySQL 1062 error (ER_DUP_ENTRY). - "UNIQUE constraint failed", // SQLite. - "duplicate key value violates unique constraint", // PostgreSQL. - } - ) - if _, ok := err.(*sqlgraph.ConstraintError); ok { - return &ConstraintError{msg, err}, true - } - for i := range errors { - if strings.Contains(msg, errors[i]) { - return &ConstraintError{msg, err}, true - } + if sqlgraph.IsConstraintError(err) { + return &ConstraintError{err.Error(), err}, true } return nil, false } diff --git a/entc/integration/config/ent/ent.go b/entc/integration/config/ent/ent.go index 4063a8a00..28dee448a 100644 --- a/entc/integration/config/ent/ent.go +++ b/entc/integration/config/ent/ent.go @@ -9,7 +9,6 @@ package ent import ( "errors" "fmt" - "strings" "entgo.io/ent" "entgo.io/ent/dialect" @@ -242,22 +241,8 @@ func IsConstraintError(err error) bool { } func isSQLConstraintError(err error) (*ConstraintError, bool) { - var ( - msg = err.Error() - // error format per dialect. - errors = [...]string{ - "Error 1062", // MySQL 1062 error (ER_DUP_ENTRY). - "UNIQUE constraint failed", // SQLite. - "duplicate key value violates unique constraint", // PostgreSQL. - } - ) - if _, ok := err.(*sqlgraph.ConstraintError); ok { - return &ConstraintError{msg, err}, true - } - for i := range errors { - if strings.Contains(msg, errors[i]) { - return &ConstraintError{msg, err}, true - } + if sqlgraph.IsConstraintError(err) { + return &ConstraintError{err.Error(), err}, true } return nil, false } diff --git a/entc/integration/customid/ent/ent.go b/entc/integration/customid/ent/ent.go index 4063a8a00..28dee448a 100644 --- a/entc/integration/customid/ent/ent.go +++ b/entc/integration/customid/ent/ent.go @@ -9,7 +9,6 @@ package ent import ( "errors" "fmt" - "strings" "entgo.io/ent" "entgo.io/ent/dialect" @@ -242,22 +241,8 @@ func IsConstraintError(err error) bool { } func isSQLConstraintError(err error) (*ConstraintError, bool) { - var ( - msg = err.Error() - // error format per dialect. - errors = [...]string{ - "Error 1062", // MySQL 1062 error (ER_DUP_ENTRY). - "UNIQUE constraint failed", // SQLite. - "duplicate key value violates unique constraint", // PostgreSQL. - } - ) - if _, ok := err.(*sqlgraph.ConstraintError); ok { - return &ConstraintError{msg, err}, true - } - for i := range errors { - if strings.Contains(msg, errors[i]) { - return &ConstraintError{msg, err}, true - } + if sqlgraph.IsConstraintError(err) { + return &ConstraintError{err.Error(), err}, true } return nil, false } diff --git a/entc/integration/edgefield/ent/ent.go b/entc/integration/edgefield/ent/ent.go index 4063a8a00..28dee448a 100644 --- a/entc/integration/edgefield/ent/ent.go +++ b/entc/integration/edgefield/ent/ent.go @@ -9,7 +9,6 @@ package ent import ( "errors" "fmt" - "strings" "entgo.io/ent" "entgo.io/ent/dialect" @@ -242,22 +241,8 @@ func IsConstraintError(err error) bool { } func isSQLConstraintError(err error) (*ConstraintError, bool) { - var ( - msg = err.Error() - // error format per dialect. - errors = [...]string{ - "Error 1062", // MySQL 1062 error (ER_DUP_ENTRY). - "UNIQUE constraint failed", // SQLite. - "duplicate key value violates unique constraint", // PostgreSQL. - } - ) - if _, ok := err.(*sqlgraph.ConstraintError); ok { - return &ConstraintError{msg, err}, true - } - for i := range errors { - if strings.Contains(msg, errors[i]) { - return &ConstraintError{msg, err}, true - } + if sqlgraph.IsConstraintError(err) { + return &ConstraintError{err.Error(), err}, true } return nil, false } diff --git a/entc/integration/ent/ent.go b/entc/integration/ent/ent.go index 4063a8a00..28dee448a 100644 --- a/entc/integration/ent/ent.go +++ b/entc/integration/ent/ent.go @@ -9,7 +9,6 @@ package ent import ( "errors" "fmt" - "strings" "entgo.io/ent" "entgo.io/ent/dialect" @@ -242,22 +241,8 @@ func IsConstraintError(err error) bool { } func isSQLConstraintError(err error) (*ConstraintError, bool) { - var ( - msg = err.Error() - // error format per dialect. - errors = [...]string{ - "Error 1062", // MySQL 1062 error (ER_DUP_ENTRY). - "UNIQUE constraint failed", // SQLite. - "duplicate key value violates unique constraint", // PostgreSQL. - } - ) - if _, ok := err.(*sqlgraph.ConstraintError); ok { - return &ConstraintError{msg, err}, true - } - for i := range errors { - if strings.Contains(msg, errors[i]) { - return &ConstraintError{msg, err}, true - } + if sqlgraph.IsConstraintError(err) { + return &ConstraintError{err.Error(), err}, true } return nil, false } diff --git a/entc/integration/hooks/ent/ent.go b/entc/integration/hooks/ent/ent.go index 4063a8a00..28dee448a 100644 --- a/entc/integration/hooks/ent/ent.go +++ b/entc/integration/hooks/ent/ent.go @@ -9,7 +9,6 @@ package ent import ( "errors" "fmt" - "strings" "entgo.io/ent" "entgo.io/ent/dialect" @@ -242,22 +241,8 @@ func IsConstraintError(err error) bool { } func isSQLConstraintError(err error) (*ConstraintError, bool) { - var ( - msg = err.Error() - // error format per dialect. - errors = [...]string{ - "Error 1062", // MySQL 1062 error (ER_DUP_ENTRY). - "UNIQUE constraint failed", // SQLite. - "duplicate key value violates unique constraint", // PostgreSQL. - } - ) - if _, ok := err.(*sqlgraph.ConstraintError); ok { - return &ConstraintError{msg, err}, true - } - for i := range errors { - if strings.Contains(msg, errors[i]) { - return &ConstraintError{msg, err}, true - } + if sqlgraph.IsConstraintError(err) { + return &ConstraintError{err.Error(), err}, true } return nil, false } diff --git a/entc/integration/idtype/ent/ent.go b/entc/integration/idtype/ent/ent.go index 4063a8a00..28dee448a 100644 --- a/entc/integration/idtype/ent/ent.go +++ b/entc/integration/idtype/ent/ent.go @@ -9,7 +9,6 @@ package ent import ( "errors" "fmt" - "strings" "entgo.io/ent" "entgo.io/ent/dialect" @@ -242,22 +241,8 @@ func IsConstraintError(err error) bool { } func isSQLConstraintError(err error) (*ConstraintError, bool) { - var ( - msg = err.Error() - // error format per dialect. - errors = [...]string{ - "Error 1062", // MySQL 1062 error (ER_DUP_ENTRY). - "UNIQUE constraint failed", // SQLite. - "duplicate key value violates unique constraint", // PostgreSQL. - } - ) - if _, ok := err.(*sqlgraph.ConstraintError); ok { - return &ConstraintError{msg, err}, true - } - for i := range errors { - if strings.Contains(msg, errors[i]) { - return &ConstraintError{msg, err}, true - } + if sqlgraph.IsConstraintError(err) { + return &ConstraintError{err.Error(), err}, true } return nil, false } diff --git a/entc/integration/integration_test.go b/entc/integration/integration_test.go index 304003236..b3dc1936a 100644 --- a/entc/integration/integration_test.go +++ b/entc/integration/integration_test.go @@ -1355,15 +1355,16 @@ func CreateBulk(t *testing.T, client *ent.Client) { } func ConstraintChecks(t *testing.T, client *ent.Client) { + var cerr *ent.ConstraintError _, err := client.Pet.Create().SetName("orphan").SetOwnerID(0).Save(context.Background()) - require.True(t, sqlgraph.IsConstraintError(err)) + require.True(t, errors.As(err, &cerr)) require.True(t, sqlgraph.IsForeignKeyConstraintError(err)) require.False(t, sqlgraph.IsUniqueConstraintError(err)) client.FileType.Create().SetName("a unique name").SaveX(context.Background()) _, err = client.FileType.Create().SetName("a unique name").Save(context.Background()) t.Logf("err %T %+v", err, err) - require.True(t, sqlgraph.IsConstraintError(err)) + require.True(t, errors.As(err, &cerr)) require.False(t, sqlgraph.IsForeignKeyConstraintError(err)) require.True(t, sqlgraph.IsUniqueConstraintError(err)) } diff --git a/entc/integration/json/ent/ent.go b/entc/integration/json/ent/ent.go index 4063a8a00..28dee448a 100644 --- a/entc/integration/json/ent/ent.go +++ b/entc/integration/json/ent/ent.go @@ -9,7 +9,6 @@ package ent import ( "errors" "fmt" - "strings" "entgo.io/ent" "entgo.io/ent/dialect" @@ -242,22 +241,8 @@ func IsConstraintError(err error) bool { } func isSQLConstraintError(err error) (*ConstraintError, bool) { - var ( - msg = err.Error() - // error format per dialect. - errors = [...]string{ - "Error 1062", // MySQL 1062 error (ER_DUP_ENTRY). - "UNIQUE constraint failed", // SQLite. - "duplicate key value violates unique constraint", // PostgreSQL. - } - ) - if _, ok := err.(*sqlgraph.ConstraintError); ok { - return &ConstraintError{msg, err}, true - } - for i := range errors { - if strings.Contains(msg, errors[i]) { - return &ConstraintError{msg, err}, true - } + if sqlgraph.IsConstraintError(err) { + return &ConstraintError{err.Error(), err}, true } return nil, false } diff --git a/entc/integration/migrate/entv1/ent.go b/entc/integration/migrate/entv1/ent.go index 35d295765..114f78381 100644 --- a/entc/integration/migrate/entv1/ent.go +++ b/entc/integration/migrate/entv1/ent.go @@ -9,7 +9,6 @@ package entv1 import ( "errors" "fmt" - "strings" "entgo.io/ent" "entgo.io/ent/dialect" @@ -242,22 +241,8 @@ func IsConstraintError(err error) bool { } func isSQLConstraintError(err error) (*ConstraintError, bool) { - var ( - msg = err.Error() - // error format per dialect. - errors = [...]string{ - "Error 1062", // MySQL 1062 error (ER_DUP_ENTRY). - "UNIQUE constraint failed", // SQLite. - "duplicate key value violates unique constraint", // PostgreSQL. - } - ) - if _, ok := err.(*sqlgraph.ConstraintError); ok { - return &ConstraintError{msg, err}, true - } - for i := range errors { - if strings.Contains(msg, errors[i]) { - return &ConstraintError{msg, err}, true - } + if sqlgraph.IsConstraintError(err) { + return &ConstraintError{err.Error(), err}, true } return nil, false } diff --git a/entc/integration/migrate/entv2/ent.go b/entc/integration/migrate/entv2/ent.go index f11e88e60..b802e2d89 100644 --- a/entc/integration/migrate/entv2/ent.go +++ b/entc/integration/migrate/entv2/ent.go @@ -9,7 +9,6 @@ package entv2 import ( "errors" "fmt" - "strings" "entgo.io/ent" "entgo.io/ent/dialect" @@ -242,22 +241,8 @@ func IsConstraintError(err error) bool { } func isSQLConstraintError(err error) (*ConstraintError, bool) { - var ( - msg = err.Error() - // error format per dialect. - errors = [...]string{ - "Error 1062", // MySQL 1062 error (ER_DUP_ENTRY). - "UNIQUE constraint failed", // SQLite. - "duplicate key value violates unique constraint", // PostgreSQL. - } - ) - if _, ok := err.(*sqlgraph.ConstraintError); ok { - return &ConstraintError{msg, err}, true - } - for i := range errors { - if strings.Contains(msg, errors[i]) { - return &ConstraintError{msg, err}, true - } + if sqlgraph.IsConstraintError(err) { + return &ConstraintError{err.Error(), err}, true } return nil, false } diff --git a/entc/integration/multischema/ent/ent.go b/entc/integration/multischema/ent/ent.go index 4063a8a00..28dee448a 100644 --- a/entc/integration/multischema/ent/ent.go +++ b/entc/integration/multischema/ent/ent.go @@ -9,7 +9,6 @@ package ent import ( "errors" "fmt" - "strings" "entgo.io/ent" "entgo.io/ent/dialect" @@ -242,22 +241,8 @@ func IsConstraintError(err error) bool { } func isSQLConstraintError(err error) (*ConstraintError, bool) { - var ( - msg = err.Error() - // error format per dialect. - errors = [...]string{ - "Error 1062", // MySQL 1062 error (ER_DUP_ENTRY). - "UNIQUE constraint failed", // SQLite. - "duplicate key value violates unique constraint", // PostgreSQL. - } - ) - if _, ok := err.(*sqlgraph.ConstraintError); ok { - return &ConstraintError{msg, err}, true - } - for i := range errors { - if strings.Contains(msg, errors[i]) { - return &ConstraintError{msg, err}, true - } + if sqlgraph.IsConstraintError(err) { + return &ConstraintError{err.Error(), err}, true } return nil, false } diff --git a/entc/integration/privacy/ent/ent.go b/entc/integration/privacy/ent/ent.go index 4063a8a00..28dee448a 100644 --- a/entc/integration/privacy/ent/ent.go +++ b/entc/integration/privacy/ent/ent.go @@ -9,7 +9,6 @@ package ent import ( "errors" "fmt" - "strings" "entgo.io/ent" "entgo.io/ent/dialect" @@ -242,22 +241,8 @@ func IsConstraintError(err error) bool { } func isSQLConstraintError(err error) (*ConstraintError, bool) { - var ( - msg = err.Error() - // error format per dialect. - errors = [...]string{ - "Error 1062", // MySQL 1062 error (ER_DUP_ENTRY). - "UNIQUE constraint failed", // SQLite. - "duplicate key value violates unique constraint", // PostgreSQL. - } - ) - if _, ok := err.(*sqlgraph.ConstraintError); ok { - return &ConstraintError{msg, err}, true - } - for i := range errors { - if strings.Contains(msg, errors[i]) { - return &ConstraintError{msg, err}, true - } + if sqlgraph.IsConstraintError(err) { + return &ConstraintError{err.Error(), err}, true } return nil, false } diff --git a/entc/integration/template/ent/ent.go b/entc/integration/template/ent/ent.go index 4063a8a00..28dee448a 100644 --- a/entc/integration/template/ent/ent.go +++ b/entc/integration/template/ent/ent.go @@ -9,7 +9,6 @@ package ent import ( "errors" "fmt" - "strings" "entgo.io/ent" "entgo.io/ent/dialect" @@ -242,22 +241,8 @@ func IsConstraintError(err error) bool { } func isSQLConstraintError(err error) (*ConstraintError, bool) { - var ( - msg = err.Error() - // error format per dialect. - errors = [...]string{ - "Error 1062", // MySQL 1062 error (ER_DUP_ENTRY). - "UNIQUE constraint failed", // SQLite. - "duplicate key value violates unique constraint", // PostgreSQL. - } - ) - if _, ok := err.(*sqlgraph.ConstraintError); ok { - return &ConstraintError{msg, err}, true - } - for i := range errors { - if strings.Contains(msg, errors[i]) { - return &ConstraintError{msg, err}, true - } + if sqlgraph.IsConstraintError(err) { + return &ConstraintError{err.Error(), err}, true } return nil, false } diff --git a/examples/edgeindex/ent/ent.go b/examples/edgeindex/ent/ent.go index 4063a8a00..28dee448a 100644 --- a/examples/edgeindex/ent/ent.go +++ b/examples/edgeindex/ent/ent.go @@ -9,7 +9,6 @@ package ent import ( "errors" "fmt" - "strings" "entgo.io/ent" "entgo.io/ent/dialect" @@ -242,22 +241,8 @@ func IsConstraintError(err error) bool { } func isSQLConstraintError(err error) (*ConstraintError, bool) { - var ( - msg = err.Error() - // error format per dialect. - errors = [...]string{ - "Error 1062", // MySQL 1062 error (ER_DUP_ENTRY). - "UNIQUE constraint failed", // SQLite. - "duplicate key value violates unique constraint", // PostgreSQL. - } - ) - if _, ok := err.(*sqlgraph.ConstraintError); ok { - return &ConstraintError{msg, err}, true - } - for i := range errors { - if strings.Contains(msg, errors[i]) { - return &ConstraintError{msg, err}, true - } + if sqlgraph.IsConstraintError(err) { + return &ConstraintError{err.Error(), err}, true } return nil, false } diff --git a/examples/entcpkg/ent/ent.go b/examples/entcpkg/ent/ent.go index 4063a8a00..28dee448a 100644 --- a/examples/entcpkg/ent/ent.go +++ b/examples/entcpkg/ent/ent.go @@ -9,7 +9,6 @@ package ent import ( "errors" "fmt" - "strings" "entgo.io/ent" "entgo.io/ent/dialect" @@ -242,22 +241,8 @@ func IsConstraintError(err error) bool { } func isSQLConstraintError(err error) (*ConstraintError, bool) { - var ( - msg = err.Error() - // error format per dialect. - errors = [...]string{ - "Error 1062", // MySQL 1062 error (ER_DUP_ENTRY). - "UNIQUE constraint failed", // SQLite. - "duplicate key value violates unique constraint", // PostgreSQL. - } - ) - if _, ok := err.(*sqlgraph.ConstraintError); ok { - return &ConstraintError{msg, err}, true - } - for i := range errors { - if strings.Contains(msg, errors[i]) { - return &ConstraintError{msg, err}, true - } + if sqlgraph.IsConstraintError(err) { + return &ConstraintError{err.Error(), err}, true } return nil, false } diff --git a/examples/m2m2types/ent/ent.go b/examples/m2m2types/ent/ent.go index 4063a8a00..28dee448a 100644 --- a/examples/m2m2types/ent/ent.go +++ b/examples/m2m2types/ent/ent.go @@ -9,7 +9,6 @@ package ent import ( "errors" "fmt" - "strings" "entgo.io/ent" "entgo.io/ent/dialect" @@ -242,22 +241,8 @@ func IsConstraintError(err error) bool { } func isSQLConstraintError(err error) (*ConstraintError, bool) { - var ( - msg = err.Error() - // error format per dialect. - errors = [...]string{ - "Error 1062", // MySQL 1062 error (ER_DUP_ENTRY). - "UNIQUE constraint failed", // SQLite. - "duplicate key value violates unique constraint", // PostgreSQL. - } - ) - if _, ok := err.(*sqlgraph.ConstraintError); ok { - return &ConstraintError{msg, err}, true - } - for i := range errors { - if strings.Contains(msg, errors[i]) { - return &ConstraintError{msg, err}, true - } + if sqlgraph.IsConstraintError(err) { + return &ConstraintError{err.Error(), err}, true } return nil, false } diff --git a/examples/m2mbidi/ent/ent.go b/examples/m2mbidi/ent/ent.go index 4063a8a00..28dee448a 100644 --- a/examples/m2mbidi/ent/ent.go +++ b/examples/m2mbidi/ent/ent.go @@ -9,7 +9,6 @@ package ent import ( "errors" "fmt" - "strings" "entgo.io/ent" "entgo.io/ent/dialect" @@ -242,22 +241,8 @@ func IsConstraintError(err error) bool { } func isSQLConstraintError(err error) (*ConstraintError, bool) { - var ( - msg = err.Error() - // error format per dialect. - errors = [...]string{ - "Error 1062", // MySQL 1062 error (ER_DUP_ENTRY). - "UNIQUE constraint failed", // SQLite. - "duplicate key value violates unique constraint", // PostgreSQL. - } - ) - if _, ok := err.(*sqlgraph.ConstraintError); ok { - return &ConstraintError{msg, err}, true - } - for i := range errors { - if strings.Contains(msg, errors[i]) { - return &ConstraintError{msg, err}, true - } + if sqlgraph.IsConstraintError(err) { + return &ConstraintError{err.Error(), err}, true } return nil, false } diff --git a/examples/m2mrecur/ent/ent.go b/examples/m2mrecur/ent/ent.go index 4063a8a00..28dee448a 100644 --- a/examples/m2mrecur/ent/ent.go +++ b/examples/m2mrecur/ent/ent.go @@ -9,7 +9,6 @@ package ent import ( "errors" "fmt" - "strings" "entgo.io/ent" "entgo.io/ent/dialect" @@ -242,22 +241,8 @@ func IsConstraintError(err error) bool { } func isSQLConstraintError(err error) (*ConstraintError, bool) { - var ( - msg = err.Error() - // error format per dialect. - errors = [...]string{ - "Error 1062", // MySQL 1062 error (ER_DUP_ENTRY). - "UNIQUE constraint failed", // SQLite. - "duplicate key value violates unique constraint", // PostgreSQL. - } - ) - if _, ok := err.(*sqlgraph.ConstraintError); ok { - return &ConstraintError{msg, err}, true - } - for i := range errors { - if strings.Contains(msg, errors[i]) { - return &ConstraintError{msg, err}, true - } + if sqlgraph.IsConstraintError(err) { + return &ConstraintError{err.Error(), err}, true } return nil, false } diff --git a/examples/o2m2types/ent/ent.go b/examples/o2m2types/ent/ent.go index 4063a8a00..28dee448a 100644 --- a/examples/o2m2types/ent/ent.go +++ b/examples/o2m2types/ent/ent.go @@ -9,7 +9,6 @@ package ent import ( "errors" "fmt" - "strings" "entgo.io/ent" "entgo.io/ent/dialect" @@ -242,22 +241,8 @@ func IsConstraintError(err error) bool { } func isSQLConstraintError(err error) (*ConstraintError, bool) { - var ( - msg = err.Error() - // error format per dialect. - errors = [...]string{ - "Error 1062", // MySQL 1062 error (ER_DUP_ENTRY). - "UNIQUE constraint failed", // SQLite. - "duplicate key value violates unique constraint", // PostgreSQL. - } - ) - if _, ok := err.(*sqlgraph.ConstraintError); ok { - return &ConstraintError{msg, err}, true - } - for i := range errors { - if strings.Contains(msg, errors[i]) { - return &ConstraintError{msg, err}, true - } + if sqlgraph.IsConstraintError(err) { + return &ConstraintError{err.Error(), err}, true } return nil, false } diff --git a/examples/o2mrecur/ent/ent.go b/examples/o2mrecur/ent/ent.go index 4063a8a00..28dee448a 100644 --- a/examples/o2mrecur/ent/ent.go +++ b/examples/o2mrecur/ent/ent.go @@ -9,7 +9,6 @@ package ent import ( "errors" "fmt" - "strings" "entgo.io/ent" "entgo.io/ent/dialect" @@ -242,22 +241,8 @@ func IsConstraintError(err error) bool { } func isSQLConstraintError(err error) (*ConstraintError, bool) { - var ( - msg = err.Error() - // error format per dialect. - errors = [...]string{ - "Error 1062", // MySQL 1062 error (ER_DUP_ENTRY). - "UNIQUE constraint failed", // SQLite. - "duplicate key value violates unique constraint", // PostgreSQL. - } - ) - if _, ok := err.(*sqlgraph.ConstraintError); ok { - return &ConstraintError{msg, err}, true - } - for i := range errors { - if strings.Contains(msg, errors[i]) { - return &ConstraintError{msg, err}, true - } + if sqlgraph.IsConstraintError(err) { + return &ConstraintError{err.Error(), err}, true } return nil, false } diff --git a/examples/o2o2types/ent/ent.go b/examples/o2o2types/ent/ent.go index 4063a8a00..28dee448a 100644 --- a/examples/o2o2types/ent/ent.go +++ b/examples/o2o2types/ent/ent.go @@ -9,7 +9,6 @@ package ent import ( "errors" "fmt" - "strings" "entgo.io/ent" "entgo.io/ent/dialect" @@ -242,22 +241,8 @@ func IsConstraintError(err error) bool { } func isSQLConstraintError(err error) (*ConstraintError, bool) { - var ( - msg = err.Error() - // error format per dialect. - errors = [...]string{ - "Error 1062", // MySQL 1062 error (ER_DUP_ENTRY). - "UNIQUE constraint failed", // SQLite. - "duplicate key value violates unique constraint", // PostgreSQL. - } - ) - if _, ok := err.(*sqlgraph.ConstraintError); ok { - return &ConstraintError{msg, err}, true - } - for i := range errors { - if strings.Contains(msg, errors[i]) { - return &ConstraintError{msg, err}, true - } + if sqlgraph.IsConstraintError(err) { + return &ConstraintError{err.Error(), err}, true } return nil, false } diff --git a/examples/o2obidi/ent/ent.go b/examples/o2obidi/ent/ent.go index 4063a8a00..28dee448a 100644 --- a/examples/o2obidi/ent/ent.go +++ b/examples/o2obidi/ent/ent.go @@ -9,7 +9,6 @@ package ent import ( "errors" "fmt" - "strings" "entgo.io/ent" "entgo.io/ent/dialect" @@ -242,22 +241,8 @@ func IsConstraintError(err error) bool { } func isSQLConstraintError(err error) (*ConstraintError, bool) { - var ( - msg = err.Error() - // error format per dialect. - errors = [...]string{ - "Error 1062", // MySQL 1062 error (ER_DUP_ENTRY). - "UNIQUE constraint failed", // SQLite. - "duplicate key value violates unique constraint", // PostgreSQL. - } - ) - if _, ok := err.(*sqlgraph.ConstraintError); ok { - return &ConstraintError{msg, err}, true - } - for i := range errors { - if strings.Contains(msg, errors[i]) { - return &ConstraintError{msg, err}, true - } + if sqlgraph.IsConstraintError(err) { + return &ConstraintError{err.Error(), err}, true } return nil, false } diff --git a/examples/o2orecur/ent/ent.go b/examples/o2orecur/ent/ent.go index 4063a8a00..28dee448a 100644 --- a/examples/o2orecur/ent/ent.go +++ b/examples/o2orecur/ent/ent.go @@ -9,7 +9,6 @@ package ent import ( "errors" "fmt" - "strings" "entgo.io/ent" "entgo.io/ent/dialect" @@ -242,22 +241,8 @@ func IsConstraintError(err error) bool { } func isSQLConstraintError(err error) (*ConstraintError, bool) { - var ( - msg = err.Error() - // error format per dialect. - errors = [...]string{ - "Error 1062", // MySQL 1062 error (ER_DUP_ENTRY). - "UNIQUE constraint failed", // SQLite. - "duplicate key value violates unique constraint", // PostgreSQL. - } - ) - if _, ok := err.(*sqlgraph.ConstraintError); ok { - return &ConstraintError{msg, err}, true - } - for i := range errors { - if strings.Contains(msg, errors[i]) { - return &ConstraintError{msg, err}, true - } + if sqlgraph.IsConstraintError(err) { + return &ConstraintError{err.Error(), err}, true } return nil, false } diff --git a/examples/privacyadmin/ent/ent.go b/examples/privacyadmin/ent/ent.go index 4063a8a00..28dee448a 100644 --- a/examples/privacyadmin/ent/ent.go +++ b/examples/privacyadmin/ent/ent.go @@ -9,7 +9,6 @@ package ent import ( "errors" "fmt" - "strings" "entgo.io/ent" "entgo.io/ent/dialect" @@ -242,22 +241,8 @@ func IsConstraintError(err error) bool { } func isSQLConstraintError(err error) (*ConstraintError, bool) { - var ( - msg = err.Error() - // error format per dialect. - errors = [...]string{ - "Error 1062", // MySQL 1062 error (ER_DUP_ENTRY). - "UNIQUE constraint failed", // SQLite. - "duplicate key value violates unique constraint", // PostgreSQL. - } - ) - if _, ok := err.(*sqlgraph.ConstraintError); ok { - return &ConstraintError{msg, err}, true - } - for i := range errors { - if strings.Contains(msg, errors[i]) { - return &ConstraintError{msg, err}, true - } + if sqlgraph.IsConstraintError(err) { + return &ConstraintError{err.Error(), err}, true } return nil, false } diff --git a/examples/privacytenant/ent/ent.go b/examples/privacytenant/ent/ent.go index 4063a8a00..28dee448a 100644 --- a/examples/privacytenant/ent/ent.go +++ b/examples/privacytenant/ent/ent.go @@ -9,7 +9,6 @@ package ent import ( "errors" "fmt" - "strings" "entgo.io/ent" "entgo.io/ent/dialect" @@ -242,22 +241,8 @@ func IsConstraintError(err error) bool { } func isSQLConstraintError(err error) (*ConstraintError, bool) { - var ( - msg = err.Error() - // error format per dialect. - errors = [...]string{ - "Error 1062", // MySQL 1062 error (ER_DUP_ENTRY). - "UNIQUE constraint failed", // SQLite. - "duplicate key value violates unique constraint", // PostgreSQL. - } - ) - if _, ok := err.(*sqlgraph.ConstraintError); ok { - return &ConstraintError{msg, err}, true - } - for i := range errors { - if strings.Contains(msg, errors[i]) { - return &ConstraintError{msg, err}, true - } + if sqlgraph.IsConstraintError(err) { + return &ConstraintError{err.Error(), err}, true } return nil, false } diff --git a/examples/start/ent/ent.go b/examples/start/ent/ent.go index 4063a8a00..28dee448a 100644 --- a/examples/start/ent/ent.go +++ b/examples/start/ent/ent.go @@ -9,7 +9,6 @@ package ent import ( "errors" "fmt" - "strings" "entgo.io/ent" "entgo.io/ent/dialect" @@ -242,22 +241,8 @@ func IsConstraintError(err error) bool { } func isSQLConstraintError(err error) (*ConstraintError, bool) { - var ( - msg = err.Error() - // error format per dialect. - errors = [...]string{ - "Error 1062", // MySQL 1062 error (ER_DUP_ENTRY). - "UNIQUE constraint failed", // SQLite. - "duplicate key value violates unique constraint", // PostgreSQL. - } - ) - if _, ok := err.(*sqlgraph.ConstraintError); ok { - return &ConstraintError{msg, err}, true - } - for i := range errors { - if strings.Contains(msg, errors[i]) { - return &ConstraintError{msg, err}, true - } + if sqlgraph.IsConstraintError(err) { + return &ConstraintError{err.Error(), err}, true } return nil, false } diff --git a/examples/traversal/ent/ent.go b/examples/traversal/ent/ent.go index 4063a8a00..28dee448a 100644 --- a/examples/traversal/ent/ent.go +++ b/examples/traversal/ent/ent.go @@ -9,7 +9,6 @@ package ent import ( "errors" "fmt" - "strings" "entgo.io/ent" "entgo.io/ent/dialect" @@ -242,22 +241,8 @@ func IsConstraintError(err error) bool { } func isSQLConstraintError(err error) (*ConstraintError, bool) { - var ( - msg = err.Error() - // error format per dialect. - errors = [...]string{ - "Error 1062", // MySQL 1062 error (ER_DUP_ENTRY). - "UNIQUE constraint failed", // SQLite. - "duplicate key value violates unique constraint", // PostgreSQL. - } - ) - if _, ok := err.(*sqlgraph.ConstraintError); ok { - return &ConstraintError{msg, err}, true - } - for i := range errors { - if strings.Contains(msg, errors[i]) { - return &ConstraintError{msg, err}, true - } + if sqlgraph.IsConstraintError(err) { + return &ConstraintError{err.Error(), err}, true } return nil, false }