errors.go

 1package errors
 2
 3import (
 4	"fmt"
 5)
 6
 7type QueryError struct {
 8	Message       string        `json:"message"`
 9	Locations     []Location    `json:"locations,omitempty"`
10	Path          []interface{} `json:"path,omitempty"`
11	Rule          string        `json:"-"`
12	ResolverError error         `json:"-"`
13}
14
15type Location struct {
16	Line   int `json:"line"`
17	Column int `json:"column"`
18}
19
20func (a Location) Before(b Location) bool {
21	return a.Line < b.Line || (a.Line == b.Line && a.Column < b.Column)
22}
23
24func Errorf(format string, a ...interface{}) *QueryError {
25	return &QueryError{
26		Message: fmt.Sprintf(format, a...),
27	}
28}
29
30func (err *QueryError) Error() string {
31	if err == nil {
32		return "<nil>"
33	}
34	str := fmt.Sprintf("graphql: %s", err.Message)
35	for _, loc := range err.Locations {
36		str += fmt.Sprintf(" (line %d, column %d)", loc.Line, loc.Column)
37	}
38	return str
39}
40
41var _ error = &QueryError{}