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:
@@ -27,7 +27,7 @@ func ({{ $receiver }} *{{ $builder }}) sqlSave(ctx context.Context) ({{ $ret }}
|
||||
{{- if $one }}
|
||||
id, ok := {{ $mutation }}.{{ $.ID.MutationGet }}()
|
||||
if !ok {
|
||||
return {{ $zero }}, fmt.Errorf("missing {{ $.Name }}.ID for update")
|
||||
return {{ $zero }}, &ValidationError{Name: "ID", err: fmt.Errorf("missing {{ $.Name }}.ID for update")}
|
||||
}
|
||||
_spec.Node.ID.Value = id
|
||||
{{- else }}
|
||||
|
||||
Reference in New Issue
Block a user