aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/vektah/gqlgen/graphql/error.go
blob: 15e65fabab91d965250efd66cde9c4b73144ebdc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package graphql

import (
	"context"
)

// Error is the standard graphql error type described in https://facebook.github.io/graphql/draft/#sec-Errors
type Error struct {
	Message    string                 `json:"message"`
	Path       []interface{}          `json:"path,omitempty"`
	Locations  []ErrorLocation        `json:"locations,omitempty"`
	Extensions map[string]interface{} `json:"extensions,omitempty"`
}

func (e *Error) Error() string {
	return e.Message
}

type ErrorLocation struct {
	Line   int `json:"line,omitempty"`
	Column int `json:"column,omitempty"`
}

type ErrorPresenterFunc func(context.Context, error) *Error

type ExtendedError interface {
	Extensions() map[string]interface{}
}

func DefaultErrorPresenter(ctx context.Context, err error) *Error {
	if gqlerr, ok := err.(*Error); ok {
		gqlerr.Path = GetResolverContext(ctx).Path
		return gqlerr
	}

	var extensions map[string]interface{}
	if ee, ok := err.(ExtendedError); ok {
		extensions = ee.Extensions()
	}

	return &Error{
		Message:    err.Error(),
		Path:       GetResolverContext(ctx).Path,
		Extensions: extensions,
	}
}