{{/* Copyright 2019-present Facebook Inc. All rights reserved. This source code is licensed under the Apache 2.0 license found in the LICENSE file in the root directory of this source tree. */}} {{/* 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 }}