error.go

 1package graphql
 2
 3import (
 4	"context"
 5)
 6
 7// Error is the standard graphql error type described in https://facebook.github.io/graphql/draft/#sec-Errors
 8type Error struct {
 9	Message    string                 `json:"message"`
10	Path       []interface{}          `json:"path,omitempty"`
11	Locations  []ErrorLocation        `json:"locations,omitempty"`
12	Extensions map[string]interface{} `json:"extensions,omitempty"`
13}
14
15func (e *Error) Error() string {
16	return e.Message
17}
18
19type ErrorLocation struct {
20	Line   int `json:"line,omitempty"`
21	Column int `json:"column,omitempty"`
22}
23
24type ErrorPresenterFunc func(context.Context, error) *Error
25
26type ExtendedError interface {
27	Extensions() map[string]interface{}
28}
29
30func DefaultErrorPresenter(ctx context.Context, err error) *Error {
31	if gqlerr, ok := err.(*Error); ok {
32		gqlerr.Path = GetResolverContext(ctx).Path
33		return gqlerr
34	}
35
36	var extensions map[string]interface{}
37	if ee, ok := err.(ExtendedError); ok {
38		extensions = ee.Extensions()
39	}
40
41	return &Error{
42		Message:    err.Error(),
43		Path:       GetResolverContext(ctx).Path,
44		Extensions: extensions,
45	}
46}