/
error.go
76 lines (64 loc) · 1.64 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package gqlerror
import (
"bytes"
"fmt"
"strconv"
)
// 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 []Location `json:"locations,omitempty"`
Extensions map[string]interface{} `json:"extensions,omitempty"`
Rule string `json:"-"`
}
func (err *Error) SetFile(file string) {
if file == "" {
return
}
if err.Extensions == nil {
err.Extensions = map[string]interface{}{}
}
err.Extensions["file"] = file
}
type Location struct {
Line int `json:"line,omitempty"`
Column int `json:"column,omitempty"`
}
type List []*Error
func (err *Error) Error() string {
filename, _ := err.Extensions["file"].(string)
if filename == "" {
filename = "input"
}
if len(err.Locations) > 0 {
filename += ":" + strconv.Itoa(err.Locations[0].Line)
}
return filename + " " + err.Message
}
func (errs List) Error() string {
var buf bytes.Buffer
for _, err := range errs {
buf.WriteString(err.Error())
buf.WriteByte('\n')
}
return buf.String()
}
func Errorf(message string, args ...interface{}) *Error {
return &Error{
Message: fmt.Sprintf(message, args...),
}
}
func ErrorLocf(file string, line int, col int, message string, args ...interface{}) *Error {
var extensions map[string]interface{}
if file != "" {
extensions = map[string]interface{}{"file": file}
}
return &Error{
Message: fmt.Sprintf(message, args...),
Extensions: extensions,
Locations: []Location{
{Line: line, Column: col},
},
}
}