mirror of
https://github.com/ent/ent.git
synced 2026-05-24 09:31:56 +03:00
entc/gen: introduce validation errors (#547)
This introduces the concept of validation errors, where we have a high
level validation error which wraps a more detailed error message.
The higher level `ValidationError` is set up in the generated files, much
like the `NotFoundError` and `ConstraintError` and is accompanied by an
`IsValidationError` check method. Thus, it can be used as follows:
```go
t, err := tx.Team.Create().SetName(input.Name).Save(ctx)
if ent.IsValidationError(err) {
// handle validation error response
}
```
This commit is contained in:
@@ -100,6 +100,31 @@ func Sum(field string) AggregateFunc {
|
||||
}
|
||||
}
|
||||
|
||||
// ValidationError returns when validating a field fails.
|
||||
type ValidationError struct {
|
||||
Name string // Field or edge name.
|
||||
err error
|
||||
}
|
||||
|
||||
// Error implements the error interface.
|
||||
func (e *ValidationError) Error() string {
|
||||
return e.err.Error()
|
||||
}
|
||||
|
||||
// Unwrap implements the errors.Wrapper interface.
|
||||
func (e *ValidationError) Unwrap() error {
|
||||
return e.err
|
||||
}
|
||||
|
||||
// IsValidationError returns a boolean indicating whether the error is a validaton error.
|
||||
func IsValidationError(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
var e *ValidationError
|
||||
return errors.As(err, &e)
|
||||
}
|
||||
|
||||
// NotFoundError returns when trying to fetch a specific entity and it was not found in the database.
|
||||
type NotFoundError struct {
|
||||
label string
|
||||
|
||||
@@ -53,7 +53,7 @@ func (gc *GroupCreate) Mutation() *GroupMutation {
|
||||
// Save creates the Group in the database.
|
||||
func (gc *GroupCreate) Save(ctx context.Context) (*Group, error) {
|
||||
if _, ok := gc.mutation.Name(); !ok {
|
||||
return nil, errors.New("ent: missing required field \"name\"")
|
||||
return nil, &ValidationError{Name: "name", err: errors.New("ent: missing required field \"name\"")}
|
||||
}
|
||||
var (
|
||||
err error
|
||||
|
||||
@@ -312,7 +312,7 @@ func (guo *GroupUpdateOne) sqlSave(ctx context.Context) (gr *Group, err error) {
|
||||
}
|
||||
id, ok := guo.mutation.ID()
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("missing Group.ID for update")
|
||||
return nil, &ValidationError{Name: "ID", err: fmt.Errorf("missing Group.ID for update")}
|
||||
}
|
||||
_spec.Node.ID.Value = id
|
||||
if value, ok := guo.mutation.Name(); ok {
|
||||
|
||||
@@ -59,10 +59,10 @@ func (uc *UserCreate) Mutation() *UserMutation {
|
||||
// Save creates the User in the database.
|
||||
func (uc *UserCreate) Save(ctx context.Context) (*User, error) {
|
||||
if _, ok := uc.mutation.Age(); !ok {
|
||||
return nil, errors.New("ent: missing required field \"age\"")
|
||||
return nil, &ValidationError{Name: "age", err: errors.New("ent: missing required field \"age\"")}
|
||||
}
|
||||
if _, ok := uc.mutation.Name(); !ok {
|
||||
return nil, errors.New("ent: missing required field \"name\"")
|
||||
return nil, &ValidationError{Name: "name", err: errors.New("ent: missing required field \"name\"")}
|
||||
}
|
||||
var (
|
||||
err error
|
||||
|
||||
@@ -352,7 +352,7 @@ func (uuo *UserUpdateOne) sqlSave(ctx context.Context) (u *User, err error) {
|
||||
}
|
||||
id, ok := uuo.mutation.ID()
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("missing User.ID for update")
|
||||
return nil, &ValidationError{Name: "ID", err: fmt.Errorf("missing User.ID for update")}
|
||||
}
|
||||
_spec.Node.ID.Value = id
|
||||
if value, ok := uuo.mutation.Age(); ok {
|
||||
|
||||
Reference in New Issue
Block a user