mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
all: use more go-ish error for constraint failures
This commit is contained in:
@@ -129,9 +129,7 @@ func (cc *CardCreate) sqlSave(ctx context.Context) (*Card, error) {
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
id := spec.ID.Value.(int64)
|
||||
c.ID = int(id)
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
@@ -171,7 +171,7 @@ func (cu *CardUpdate) sqlSave(ctx context.Context) (n int, err error) {
|
||||
return 0, rollback(tx, err)
|
||||
}
|
||||
if int(affected) < len(cu.owner) {
|
||||
return 0, rollback(tx, &ErrConstraintFailed{msg: fmt.Sprintf("one of \"owner\" %v already connected to a different \"Card\"", keys(cu.owner))})
|
||||
return 0, rollback(tx, &ConstraintError{msg: fmt.Sprintf("one of \"owner\" %v already connected to a different \"Card\"", keys(cu.owner))})
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -333,7 +333,7 @@ func (cuo *CardUpdateOne) sqlSave(ctx context.Context) (c *Card, err error) {
|
||||
return nil, rollback(tx, err)
|
||||
}
|
||||
if int(affected) < len(cuo.owner) {
|
||||
return nil, rollback(tx, &ErrConstraintFailed{msg: fmt.Sprintf("one of \"owner\" %v already connected to a different \"Card\"", keys(cuo.owner))})
|
||||
return nil, rollback(tx, &ConstraintError{msg: fmt.Sprintf("one of \"owner\" %v already connected to a different \"Card\"", keys(cuo.owner))})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -127,30 +127,31 @@ func IsNotSingular(err error) bool {
|
||||
return ok
|
||||
}
|
||||
|
||||
// ErrConstraintFailed returns when trying to create/update one or more entities and
|
||||
// one or more of their constraints failed. For example, violation of edge or field uniqueness.
|
||||
type ErrConstraintFailed struct {
|
||||
// ConstraintError returns when trying to create/update one or more entities and
|
||||
// one or more of their constraints failed. For example, violation of edge or
|
||||
// field uniqueness.
|
||||
type ConstraintError struct {
|
||||
msg string
|
||||
wrap error
|
||||
}
|
||||
|
||||
// Error implements the error interface.
|
||||
func (e ErrConstraintFailed) Error() string {
|
||||
return fmt.Sprintf("ent: unique constraint failed: %s", e.msg)
|
||||
func (e ConstraintError) Error() string {
|
||||
return fmt.Sprintf("ent: constraint failed: %s", e.msg)
|
||||
}
|
||||
|
||||
// Unwrap implements the errors.Wrapper interface.
|
||||
func (e *ErrConstraintFailed) Unwrap() error {
|
||||
func (e *ConstraintError) Unwrap() error {
|
||||
return e.wrap
|
||||
}
|
||||
|
||||
// IsConstraintFailure returns a boolean indicating whether the error is a constraint failure.
|
||||
func IsConstraintFailure(err error) bool {
|
||||
_, ok := err.(*ErrConstraintFailed)
|
||||
// IsConstraintError returns a boolean indicating whether the error is a constraint failure.
|
||||
func IsConstraintError(err error) bool {
|
||||
_, ok := err.(*ConstraintError)
|
||||
return ok
|
||||
}
|
||||
|
||||
func isSQLConstraintError(err error) (*ErrConstraintFailed, bool) {
|
||||
func isSQLConstraintError(err error) (*ConstraintError, bool) {
|
||||
var (
|
||||
msg = err.Error()
|
||||
// error format per dialect.
|
||||
@@ -161,11 +162,11 @@ func isSQLConstraintError(err error) (*ErrConstraintFailed, bool) {
|
||||
}
|
||||
)
|
||||
if _, ok := err.(*sqlgraph.ConstraintError); ok {
|
||||
return &ErrConstraintFailed{msg, err}, true
|
||||
return &ConstraintError{msg, err}, true
|
||||
}
|
||||
for i := range errors {
|
||||
if strings.Contains(msg, errors[i]) {
|
||||
return &ErrConstraintFailed{msg, err}, true
|
||||
return &ConstraintError{msg, err}, true
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
|
||||
@@ -133,9 +133,7 @@ func (uc *UserCreate) sqlSave(ctx context.Context) (*User, error) {
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
id := spec.ID.Value.(int64)
|
||||
u.ID = int(id)
|
||||
|
||||
return u, nil
|
||||
}
|
||||
|
||||
@@ -190,7 +190,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) {
|
||||
return 0, rollback(tx, err)
|
||||
}
|
||||
if int(affected) < len(uu.card) {
|
||||
return 0, rollback(tx, &ErrConstraintFailed{msg: fmt.Sprintf("one of \"card\" %v already connected to a different \"User\"", keys(uu.card))})
|
||||
return 0, rollback(tx, &ConstraintError{msg: fmt.Sprintf("one of \"card\" %v already connected to a different \"User\"", keys(uu.card))})
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -373,7 +373,7 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (u *User, err error) {
|
||||
return nil, rollback(tx, err)
|
||||
}
|
||||
if int(affected) < len(uuo.card) {
|
||||
return nil, rollback(tx, &ErrConstraintFailed{msg: fmt.Sprintf("one of \"card\" %v already connected to a different \"User\"", keys(uuo.card))})
|
||||
return nil, rollback(tx, &ConstraintError{msg: fmt.Sprintf("one of \"card\" %v already connected to a different \"User\"", keys(uuo.card))})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user