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
This commit is contained in:
Rotem Tamir
2021-03-10 14:07:00 +02:00
committed by GitHub
parent e52439c5be
commit bc04816c38
29 changed files with 59 additions and 462 deletions

View File

@@ -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
}