mirror of
https://github.com/ent/ent.git
synced 2026-04-28 21:50:56 +03:00
dialect/sql/sqlgraph: report check constraint violation with IsCheckConstraintError (#4404)
* feat(dialect): Support check constraint violation * chore: Add unit tests for IsCheckConstraintError
This commit is contained in:
@@ -12,7 +12,10 @@ import (
|
||||
// IsConstraintError returns true if the error resulted from a database constraint violation.
|
||||
func IsConstraintError(err error) bool {
|
||||
var e *ConstraintError
|
||||
return errors.As(err, &e) || IsUniqueConstraintError(err) || IsForeignKeyConstraintError(err)
|
||||
return errors.As(err, &e) ||
|
||||
IsUniqueConstraintError(err) ||
|
||||
IsForeignKeyConstraintError(err) ||
|
||||
IsCheckConstraintError(err)
|
||||
}
|
||||
|
||||
// IsUniqueConstraintError reports if the error resulted from a DB uniqueness constraint violation.
|
||||
@@ -51,3 +54,21 @@ func IsForeignKeyConstraintError(err error) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// IsCheckConstraintError reports if the error resulted from a database check constraint violation.
|
||||
// e.g. a value does not satisfy a check condition.
|
||||
func IsCheckConstraintError(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
for _, s := range []string{
|
||||
"Error 3819", // MySQL
|
||||
"violates check constraint", // Postgres
|
||||
"CHECK constraint failed", // SQLite
|
||||
} {
|
||||
if strings.Contains(err.Error(), s) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user