Repository navigation
Expand file tree
/
Copy patherrors.go
More file actions
122 lines (103 loc) · 3.2 KB
/
Copy patherrors.go
File metadata and controls
122 lines (103 loc) · 3.2 KB
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
package api
import (
"encoding/json"
"github.com/sourcegraph/sourcegraph/lib/errors"
)
// GraphQlErrors contains one or more GraphQlError instances.
type GraphQlErrors []*GraphQlError
// NewGraphQlErrors wraps raw GraphQL error payloads in GraphQlError values.
func NewGraphQlErrors(graphQLErrorPayloads []json.RawMessage) GraphQlErrors {
errs := make(GraphQlErrors, 0, len(graphQLErrorPayloads))
for _, graphQLErrorPayload := range graphQLErrorPayloads {
var value any
if err := json.Unmarshal(graphQLErrorPayload, &value); err != nil {
value = map[string]any{"message": string(graphQLErrorPayload)}
}
errs = append(errs, &GraphQlError{v: value})
}
return errs
}
func (gg GraphQlErrors) Error() string {
// This slightly convoluted implementation is used to ensure that output
// remains stable with earlier versions of src-cli, which returned a wrapped
// *multierror.Error when GraphQL errors were returned from the API.
if len(gg) == 0 {
// This shouldn't really happen, but let's handle it gracefully anyway.
return ""
}
var errs errors.MultiError
for _, err := range gg {
errs = errors.Append(errs, err)
}
return errors.Wrap(errs, "GraphQL errors").Error()
}
// GraphQlError wraps a raw JSON error returned from a GraphQL endpoint.
type GraphQlError struct{ v any }
// Message returns the GraphQL error message, if one was set on the error.
func (g *GraphQlError) Message() (string, error) {
e, ok := g.v.(map[string]any)
if !ok {
return "", errors.Errorf("unexpected GraphQL error of type %T", g.v)
}
if e["message"] == nil {
return "", nil
}
message, ok := e["message"].(string)
if !ok {
return "", errors.Errorf("unexpected message of type %T", e["message"])
}
return message, nil
}
// Code returns the GraphQL error code, if one was set on the error.
func (g *GraphQlError) Code() (string, error) {
ext, err := g.Extensions()
if err != nil {
return "", errors.Wrap(err, "getting error extensions")
}
if ext != nil {
if ext["code"] == nil {
return "", nil
} else if code, ok := ext["code"].(string); ok {
return code, nil
}
return "", errors.Errorf("unexpected code of type %T", ext["code"])
}
return "", nil
}
// Path returns the GraphQL error path, if one was set on the error.
func (g *GraphQlError) Path() ([]any, error) {
e, ok := g.v.(map[string]any)
if !ok {
return nil, errors.Errorf("unexpected GraphQL error of type %T", g.v)
}
if e["path"] == nil {
return nil, nil
}
path, ok := e["path"].([]any)
if !ok {
return nil, errors.Errorf("unexpected path of type %T", e["path"])
}
return path, nil
}
func (g *GraphQlError) Error() string {
j, _ := json.MarshalIndent(g.v, "", " ")
return string(j)
}
// Extensions returns the GraphQL error extensions, if set, or nil if no
// extensions were set on the error.
func (g *GraphQlError) Extensions() (map[string]any, error) {
e, ok := g.v.(map[string]any)
if !ok {
return nil, errors.Errorf("unexpected GraphQL error of type %T", g.v)
}
if e["extensions"] == nil {
return nil, nil
} else if me, ok := e["extensions"].(map[string]any); ok {
return me, nil
}
return nil, errors.Errorf("unexpected extensions of type %T", e["extensions"])
}
var (
_ error = &GraphQlError{}
_ error = GraphQlErrors{}
)