-
Notifications
You must be signed in to change notification settings - Fork 103
/
errors.go
132 lines (115 loc) · 2.66 KB
/
errors.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package generate
import (
"errors"
"fmt"
"strconv"
"strings"
"github.com/vektah/gqlparser/v2/ast"
"github.com/vektah/gqlparser/v2/gqlerror"
)
type errorPos struct {
filename string
line, col int
}
func (pos *errorPos) String() string {
filename, lineOffset := splitFilename(pos.filename)
line := lineOffset + pos.line
if line != 0 {
return fmt.Sprintf("%v:%v", filename, line)
} else {
return filename
}
}
type genqlientError struct {
pos *errorPos
msg string
wrapped error
}
func splitFilename(filename string) (name string, lineOffset int) {
split := strings.Split(filename, ":")
if len(split) != 2 {
return filename, 0
}
offset, err := strconv.Atoi(split[1])
if err != nil {
return split[0], 0
}
return split[0], offset - 1
}
func (err *genqlientError) Error() string {
if err.pos != nil {
return err.pos.String() + ": " + err.msg
} else {
return err.msg
}
}
func (err *genqlientError) Unwrap() error {
return err.wrapped
}
func errorf(pos *ast.Position, msg string, args ...interface{}) error {
// TODO: alternately accept a filename only, or maybe even a go-parser pos
// We do all our own wrapping, because if the wrapped error already has a
// pos, we want to extract it out and put it at the front, not in the
// middle.
var wrapped error
var wrapIndex int
for i, arg := range args {
if wrapped == nil {
var ok bool
wrapped, ok = arg.(error)
if ok {
wrapIndex = i
}
}
}
var wrappedGenqlient *genqlientError
isGenqlient := errors.As(wrapped, &wrappedGenqlient)
var wrappedGraphQL *gqlerror.Error
isGraphQL := errors.As(wrapped, &wrappedGraphQL)
if !isGraphQL {
var wrappedGraphQLList gqlerror.List
isGraphQLList := errors.As(wrapped, &wrappedGraphQLList)
if isGraphQLList && len(wrappedGraphQLList) > 0 {
isGraphQL = true
wrappedGraphQL = wrappedGraphQLList[0]
}
}
var errPos *errorPos
if pos != nil {
errPos = &errorPos{
filename: pos.Src.Name,
line: pos.Line,
col: pos.Column,
}
} else if isGenqlient {
errPos = wrappedGenqlient.pos
} else if isGraphQL {
filename, _ := wrappedGraphQL.Extensions["file"].(string)
if filename != "" {
var loc gqlerror.Location
if len(wrappedGraphQL.Locations) > 0 {
loc = wrappedGraphQL.Locations[0]
}
errPos = &errorPos{
filename: filename,
line: loc.Line,
col: loc.Column,
}
}
}
if wrapped != nil {
errText := wrapped.Error()
if isGenqlient {
errText = wrappedGenqlient.msg
} else if isGraphQL {
errText = wrappedGraphQL.Message
}
args[wrapIndex] = errText
}
msg = fmt.Sprintf(msg, args...)
return &genqlientError{
msg: msg,
pos: errPos,
wrapped: wrapped,
}
}