forked from graphql-go/graphql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherror.go
61 lines (56 loc) · 1.38 KB
/
error.go
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package gqlerrors
import (
"fmt"
"github.com/graphql-go/graphql/language/ast"
"github.com/graphql-go/graphql/language/location"
"github.com/graphql-go/graphql/language/source"
)
type Error struct {
Message string
Stack string
Nodes []ast.Node
Source *source.Source
Positions []int
Locations []location.SourceLocation
OriginalError error
}
// implements Golang's built-in `error` interface
func (g Error) Error() string {
return fmt.Sprintf("%v", g.Message)
}
func NewError(message string, nodes []ast.Node, stack string, source *source.Source, positions []int, origError error) *Error {
if stack == "" && message != "" {
stack = message
}
if source == nil {
for _, node := range nodes {
// get source from first node
if node.GetLoc() != nil {
source = node.GetLoc().Source
}
break
}
}
if len(positions) == 0 && len(nodes) > 0 {
for _, node := range nodes {
if node.GetLoc() == nil {
continue
}
positions = append(positions, node.GetLoc().Start)
}
}
locations := []location.SourceLocation{}
for _, pos := range positions {
loc := location.GetLocation(source, pos)
locations = append(locations, loc)
}
return &Error{
Message: message,
Stack: stack,
Nodes: nodes,
Source: source,
Positions: positions,
Locations: locations,
OriginalError: origError,
}
}