-
Notifications
You must be signed in to change notification settings - Fork 89
/
graphql.go
112 lines (93 loc) · 2.77 KB
/
graphql.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
package http
import (
"net/http"
"strings"
)
type graphQLRequest struct {
Query string `json:"query"`
Variables map[string]interface{} `json:"variables,omitempty"`
}
type graphQLResponse struct {
Data interface{} `json:"data"`
}
// GraphQLError represents a single error.
type GraphQLError struct {
Message string `json:"message,omitempty"`
Path []string `json:"path,omitempty"`
Extensions struct {
ErrorClass string `json:"errorClass,omitempty"`
ErrorCode string `json:"error_code,omitempty"`
Code string `json:"code,omitempty"`
} `json:"extensions,omitempty"`
}
// GraphQLErrorResponse represents a default error response body.
type GraphQLErrorResponse struct {
Errors []GraphQLError `json:"errors"`
}
func (r *GraphQLErrorResponse) Error() string {
if len(r.Errors) > 0 {
messages := []string{}
for _, e := range r.Errors {
if e.Message != "" {
messages = append(messages, e.Message)
}
}
return strings.Join(messages, ", ")
}
return ""
}
// IsNotFound determines if the error is due to a missing resource.
func (r *GraphQLErrorResponse) IsNotFound() bool {
return false
}
// IsRetryableError determines if the error is due to a server timeout, or another error that we might want to retry.
func (r *GraphQLErrorResponse) IsRetryableError() bool {
if len(r.Errors) == 0 {
return false
}
for _, err := range r.Errors {
errorClass := err.Extensions.ErrorClass
if errorClass == "TIMEOUT" || errorClass == "INTERNAL_SERVER_ERROR" || errorClass == "SERVER_ERROR" {
return true
}
}
return false
}
// IsUnauthorized checks a NerdGraph response for a 401 Unauthorize HTTP status code,
// then falls back to check the nested extensions error_code field for `BAD_API_KEY`.
func (r *GraphQLErrorResponse) IsUnauthorized(resp *http.Response) bool {
if len(r.Errors) == 0 {
return false
}
if resp.StatusCode == http.StatusUnauthorized {
return true
}
// Handle invalid or missing API key
for _, err := range r.Errors {
if err.Extensions.ErrorCode == "BAD_API_KEY" {
return true
}
}
return false
}
func (r *GraphQLErrorResponse) IsPaymentRequired(resp *http.Response) bool {
return resp.StatusCode == http.StatusPaymentRequired
}
// IsDeprecated parses error messages for warnings that a field being used
// is deprecated. We want to bubble that up, but not stop returning data
//
// Example deprecation message:
//
// This field is deprecated! Please use `relatedEntities` instead.
func (r *GraphQLErrorResponse) IsDeprecated() bool {
for _, err := range r.Errors {
if strings.HasPrefix(err.Message, "This field is deprecated!") {
return true
}
}
return false
}
// New creates a new instance of GraphQLErrorRepsonse.
func (r *GraphQLErrorResponse) New() ErrorResponse {
return &GraphQLErrorResponse{}
}