Files
ent/entc/gen/template/dialect/gremlin/errors.tmpl
Ariel Mashraki 56656dfcb6 ent/entc: configure storage driver in codegen
Summary: Pull Request resolved: https://github.com/facebookexternal/fbc/pull/1229

Reviewed By: alexsn

Differential Revision: D16539934

fbshipit-source-id: b3a8bf1f1be6f65ad3f649cd921ea20fc24182bf
2019-07-30 02:49:22 -07:00

46 lines
1.6 KiB
Cheetah

{{/* custom errors and errors handlers from sql dialects */}}
{{ define "dialect/gremlin/errors" }}
{{ $pkg := base $.Config.Package }}
// Code implements the dsl.Node interface.
func (e ErrConstraintFailed) Code() (string, []interface{}) {
return strconv.Quote(e.prefix() + e.msg), nil
}
func (e *ErrConstraintFailed) UnmarshalGraphson(b []byte) error {
var v [1]*string
if err := graphson.Unmarshal(b, &v); err != nil {
return err
}
if v[0] == nil {
return fmt.Errorf("{{ $pkg }}: missing string value")
}
if !strings.HasPrefix(*v[0], e.prefix()) {
return fmt.Errorf("{{ $pkg }}: invalid string for error: %s", *v[0])
}
e.msg = strings.TrimPrefix(*v[0], e.prefix())
return nil
}
// prefix returns the prefix used for gremlin constants.
func (ErrConstraintFailed) prefix() string { return "Error: " }
// NewErrUniqueField creates a constraint error for unique fields.
func NewErrUniqueField(label, field string, v interface{}) *ErrConstraintFailed {
return &ErrConstraintFailed{msg: fmt.Sprintf("field %s.%s with value: %#v", label, field, v)}
}
// NewErrUniqueEdge creates a constraint error for unique edges.
func NewErrUniqueEdge(label, edge, id string) *ErrConstraintFailed {
return &ErrConstraintFailed{msg: fmt.Sprintf("edge %s.%s with id: %#v", label, edge, id)}
}
// isConstantError indicates if the given response holds a gremlin constant containing an error.
func isConstantError(r *gremlin.Response) (*ErrConstraintFailed, bool) {
e := &ErrConstraintFailed{}
if err := graphson.Unmarshal(r.Result.Data, e); err != nil {
return nil, false
}
return e, true
}
{{ end }}