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

@@ -110,9 +110,7 @@ func (cc *CityCreate) sqlSave(ctx context.Context) (*City, error) {
}
return nil, err
}
id := spec.ID.Value.(int64)
c.ID = int(id)
return c, nil
}

View File

@@ -181,7 +181,7 @@ func (cu *CityUpdate) sqlSave(ctx context.Context) (n int, err error) {
return 0, rollback(tx, err)
}
if int(affected) < len(cu.streets) {
return 0, rollback(tx, &ErrConstraintFailed{msg: fmt.Sprintf("one of \"streets\" %v already connected to a different \"City\"", keys(cu.streets))})
return 0, rollback(tx, &ConstraintError{msg: fmt.Sprintf("one of \"streets\" %v already connected to a different \"City\"", keys(cu.streets))})
}
}
}
@@ -354,7 +354,7 @@ func (cuo *CityUpdateOne) sqlSave(ctx context.Context) (c *City, err error) {
return nil, rollback(tx, err)
}
if int(affected) < len(cuo.streets) {
return nil, rollback(tx, &ErrConstraintFailed{msg: fmt.Sprintf("one of \"streets\" %v already connected to a different \"City\"", keys(cuo.streets))})
return nil, rollback(tx, &ConstraintError{msg: fmt.Sprintf("one of \"streets\" %v already connected to a different \"City\"", keys(cuo.streets))})
}
}
}

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

@@ -115,9 +115,7 @@ func (sc *StreetCreate) sqlSave(ctx context.Context) (*Street, error) {
}
return nil, err
}
id := spec.ID.Value.(int64)
s.ID = int(id)
return s, nil
}

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

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

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

@@ -110,9 +110,7 @@ func (gc *GroupCreate) sqlSave(ctx context.Context) (*Group, error) {
}
return nil, err
}
id := spec.ID.Value.(int64)
gr.ID = int(id)
return gr, nil
}

View File

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

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
}

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

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

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

@@ -115,9 +115,7 @@ func (pc *PetCreate) sqlSave(ctx context.Context) (*Pet, error) {
}
return nil, err
}
id := spec.ID.Value.(int64)
pe.ID = int(id)
return pe, nil
}

View File

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

View File

@@ -206,7 +206,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) {
return 0, rollback(tx, err)
}
if int(affected) < len(uu.pets) {
return 0, rollback(tx, &ErrConstraintFailed{msg: fmt.Sprintf("one of \"pets\" %v already connected to a different \"User\"", keys(uu.pets))})
return 0, rollback(tx, &ConstraintError{msg: fmt.Sprintf("one of \"pets\" %v already connected to a different \"User\"", keys(uu.pets))})
}
}
}
@@ -406,7 +406,7 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (u *User, err error) {
return nil, rollback(tx, err)
}
if int(affected) < len(uuo.pets) {
return nil, rollback(tx, &ErrConstraintFailed{msg: fmt.Sprintf("one of \"pets\" %v already connected to a different \"User\"", keys(uuo.pets))})
return nil, rollback(tx, &ConstraintError{msg: fmt.Sprintf("one of \"pets\" %v already connected to a different \"User\"", keys(uuo.pets))})
}
}
}

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

@@ -154,9 +154,7 @@ func (nc *NodeCreate) sqlSave(ctx context.Context) (*Node, error) {
}
return nil, err
}
id := spec.ID.Value.(int64)
n.ID = int(id)
return n, nil
}

View File

@@ -249,7 +249,7 @@ func (nu *NodeUpdate) sqlSave(ctx context.Context) (n int, err error) {
return 0, rollback(tx, err)
}
if int(affected) < len(nu.children) {
return 0, rollback(tx, &ErrConstraintFailed{msg: fmt.Sprintf("one of \"children\" %v already connected to a different \"Node\"", keys(nu.children))})
return 0, rollback(tx, &ConstraintError{msg: fmt.Sprintf("one of \"children\" %v already connected to a different \"Node\"", keys(nu.children))})
}
}
}
@@ -491,7 +491,7 @@ func (nuo *NodeUpdateOne) sqlSave(ctx context.Context) (n *Node, err error) {
return nil, rollback(tx, err)
}
if int(affected) < len(nuo.children) {
return nil, rollback(tx, &ErrConstraintFailed{msg: fmt.Sprintf("one of \"children\" %v already connected to a different \"Node\"", keys(nuo.children))})
return nil, rollback(tx, &ConstraintError{msg: fmt.Sprintf("one of \"children\" %v already connected to a different \"Node\"", keys(nuo.children))})
}
}
}

View File

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

View File

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

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

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

View File

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

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

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

View File

@@ -203,7 +203,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) {
return 0, rollback(tx, err)
}
if int(affected) < len(uu.spouse) {
return 0, rollback(tx, &ErrConstraintFailed{msg: fmt.Sprintf("\"spouse\" (%v) already connected to a different \"User\"", eid)})
return 0, rollback(tx, &ConstraintError{msg: fmt.Sprintf("\"spouse\" (%v) already connected to a different \"User\"", eid)})
}
}
}
@@ -400,7 +400,7 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (u *User, err error) {
return nil, rollback(tx, err)
}
if int(affected) < len(uuo.spouse) {
return nil, rollback(tx, &ErrConstraintFailed{msg: fmt.Sprintf("\"spouse\" (%v) already connected to a different \"User\"", eid)})
return nil, rollback(tx, &ConstraintError{msg: fmt.Sprintf("\"spouse\" (%v) already connected to a different \"User\"", eid)})
}
}
}

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

@@ -159,9 +159,7 @@ func (nc *NodeCreate) sqlSave(ctx context.Context) (*Node, error) {
}
return nil, err
}
id := spec.ID.Value.(int64)
n.ID = int(id)
return n, nil
}

View File

@@ -212,7 +212,7 @@ func (nu *NodeUpdate) sqlSave(ctx context.Context) (n int, err error) {
return 0, rollback(tx, err)
}
if int(affected) < len(nu.prev) {
return 0, rollback(tx, &ErrConstraintFailed{msg: fmt.Sprintf("one of \"prev\" %v already connected to a different \"Node\"", keys(nu.prev))})
return 0, rollback(tx, &ConstraintError{msg: fmt.Sprintf("one of \"prev\" %v already connected to a different \"Node\"", keys(nu.prev))})
}
}
}
@@ -240,7 +240,7 @@ func (nu *NodeUpdate) sqlSave(ctx context.Context) (n int, err error) {
return 0, rollback(tx, err)
}
if int(affected) < len(nu.next) {
return 0, rollback(tx, &ErrConstraintFailed{msg: fmt.Sprintf("one of \"next\" %v already connected to a different \"Node\"", keys(nu.next))})
return 0, rollback(tx, &ConstraintError{msg: fmt.Sprintf("one of \"next\" %v already connected to a different \"Node\"", keys(nu.next))})
}
}
}
@@ -445,7 +445,7 @@ func (nuo *NodeUpdateOne) sqlSave(ctx context.Context) (n *Node, err error) {
return nil, rollback(tx, err)
}
if int(affected) < len(nuo.prev) {
return nil, rollback(tx, &ErrConstraintFailed{msg: fmt.Sprintf("one of \"prev\" %v already connected to a different \"Node\"", keys(nuo.prev))})
return nil, rollback(tx, &ConstraintError{msg: fmt.Sprintf("one of \"prev\" %v already connected to a different \"Node\"", keys(nuo.prev))})
}
}
}
@@ -473,7 +473,7 @@ func (nuo *NodeUpdateOne) sqlSave(ctx context.Context) (n *Node, err error) {
return nil, rollback(tx, err)
}
if int(affected) < len(nuo.next) {
return nil, rollback(tx, &ErrConstraintFailed{msg: fmt.Sprintf("one of \"next\" %v already connected to a different \"Node\"", keys(nuo.next))})
return nil, rollback(tx, &ConstraintError{msg: fmt.Sprintf("one of \"next\" %v already connected to a different \"Node\"", keys(nuo.next))})
}
}
}

View File

@@ -134,9 +134,7 @@ func (cc *CarCreate) sqlSave(ctx context.Context) (*Car, error) {
}
return nil, err
}
id := spec.ID.Value.(int64)
c.ID = int(id)
return c, nil
}

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

@@ -114,9 +114,7 @@ func (gc *GroupCreate) sqlSave(ctx context.Context) (*Group, error) {
}
return nil, err
}
id := spec.ID.Value.(int64)
gr.ID = int(id)
return gr, nil
}

View File

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

View File

@@ -261,7 +261,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) {
return 0, rollback(tx, err)
}
if int(affected) < len(uu.cars) {
return 0, rollback(tx, &ErrConstraintFailed{msg: fmt.Sprintf("one of \"cars\" %v already connected to a different \"User\"", keys(uu.cars))})
return 0, rollback(tx, &ConstraintError{msg: fmt.Sprintf("one of \"cars\" %v already connected to a different \"User\"", keys(uu.cars))})
}
}
}
@@ -546,7 +546,7 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (u *User, err error) {
return nil, rollback(tx, err)
}
if int(affected) < len(uuo.cars) {
return nil, rollback(tx, &ErrConstraintFailed{msg: fmt.Sprintf("one of \"cars\" %v already connected to a different \"User\"", keys(uuo.cars))})
return nil, rollback(tx, &ConstraintError{msg: fmt.Sprintf("one of \"cars\" %v already connected to a different \"User\"", keys(uuo.cars))})
}
}
}

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

@@ -155,9 +155,7 @@ func (gc *GroupCreate) sqlSave(ctx context.Context) (*Group, error) {
}
return nil, err
}
id := spec.ID.Value.(int64)
gr.ID = int(id)
return gr, nil
}

View File

@@ -155,9 +155,7 @@ func (pc *PetCreate) sqlSave(ctx context.Context) (*Pet, error) {
}
return nil, err
}
id := spec.ID.Value.(int64)
pe.ID = int(id)
return pe, nil
}

View File

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

View File

@@ -333,7 +333,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) {
return 0, rollback(tx, err)
}
if int(affected) < len(uu.pets) {
return 0, rollback(tx, &ErrConstraintFailed{msg: fmt.Sprintf("one of \"pets\" %v already connected to a different \"User\"", keys(uu.pets))})
return 0, rollback(tx, &ConstraintError{msg: fmt.Sprintf("one of \"pets\" %v already connected to a different \"User\"", keys(uu.pets))})
}
}
}
@@ -436,7 +436,7 @@ func (uu *UserUpdate) sqlSave(ctx context.Context) (n int, err error) {
return 0, rollback(tx, err)
}
if int(affected) < len(uu.manage) {
return 0, rollback(tx, &ErrConstraintFailed{msg: fmt.Sprintf("one of \"manage\" %v already connected to a different \"User\"", keys(uu.manage))})
return 0, rollback(tx, &ConstraintError{msg: fmt.Sprintf("one of \"manage\" %v already connected to a different \"User\"", keys(uu.manage))})
}
}
}
@@ -762,7 +762,7 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (u *User, err error) {
return nil, rollback(tx, err)
}
if int(affected) < len(uuo.pets) {
return nil, rollback(tx, &ErrConstraintFailed{msg: fmt.Sprintf("one of \"pets\" %v already connected to a different \"User\"", keys(uuo.pets))})
return nil, rollback(tx, &ConstraintError{msg: fmt.Sprintf("one of \"pets\" %v already connected to a different \"User\"", keys(uuo.pets))})
}
}
}
@@ -865,7 +865,7 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (u *User, err error) {
return nil, rollback(tx, err)
}
if int(affected) < len(uuo.manage) {
return nil, rollback(tx, &ErrConstraintFailed{msg: fmt.Sprintf("one of \"manage\" %v already connected to a different \"User\"", keys(uuo.manage))})
return nil, rollback(tx, &ConstraintError{msg: fmt.Sprintf("one of \"manage\" %v already connected to a different \"User\"", keys(uuo.manage))})
}
}
}