all: use more go-ish error for constraint failures

This commit is contained in:
Ariel Mashraki
2019-12-17 22:55:33 +02:00
parent c6800a3869
commit 798d58f02b
78 changed files with 422 additions and 440 deletions

View File

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

View File

@@ -127,9 +127,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
}