mirror of
https://github.com/ent/ent.git
synced 2026-05-28 09:49:08 +03:00
ent/entc: configure storage driver in codegen
Summary: Pull Request resolved: https://github.com/facebookexternal/fbc/pull/1229 Reviewed By: alexsn Differential Revision: D16539934 fbshipit-source-id: b3a8bf1f1be6f65ad3f649cd921ea20fc24182bf
This commit is contained in:
committed by
Facebook Github Bot
parent
c6b178de20
commit
56656dfcb6
21
entc/gen/template/dialect/sql/errors.tmpl
Normal file
21
entc/gen/template/dialect/sql/errors.tmpl
Normal file
@@ -0,0 +1,21 @@
|
||||
{{/* custom errors and errors handlers from sql dialects */}}
|
||||
{{ define "dialect/sql/errors" }}
|
||||
func isSQLConstraintError(err error) (*ErrConstraintFailed, bool) {
|
||||
// Error number 1062 is ER_DUP_ENTRY in mysql, and "UNIQUE constraint failed" is SQLite prefix.
|
||||
if msg := err.Error(); strings.HasPrefix(msg, "Error 1062") || strings.HasPrefix(msg, "UNIQUE constraint failed") {
|
||||
return &ErrConstraintFailed{msg, err}, true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// rollback calls to tx.Rollback and wraps the given error with the rollback error if occurred.
|
||||
func rollback(tx dialect.Tx, err error) error {
|
||||
if rerr := tx.Rollback(); rerr != nil {
|
||||
err = fmt.Errorf("%s: %v", err.Error(), rerr)
|
||||
}
|
||||
if err, ok := isSQLConstraintError(err); ok {
|
||||
return err
|
||||
}
|
||||
return err
|
||||
}
|
||||
{{ end }}
|
||||
Reference in New Issue
Block a user