Skip to content

Commit 4a45ca6

Browse files
authored
Merge pull request cli#4154 from cli/graphql-502-error
Fix HTTP 502 error reporting from GraphQL request
2 parents c84bfa9 + 3e23dca commit 4a45ca6

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

api/client.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,10 @@ func HandleHTTPError(resp *http.Response) error {
285285
return httpError
286286
}
287287

288-
messages := []string{parsedBody.Message}
288+
var messages []string
289+
if parsedBody.Message != "" {
290+
messages = append(messages, parsedBody.Message)
291+
}
289292
for _, raw := range parsedBody.Errors {
290293
switch raw[0] {
291294
case '"':
@@ -297,7 +300,7 @@ func HandleHTTPError(resp *http.Response) error {
297300
var errInfo HTTPErrorItem
298301
_ = json.Unmarshal(raw, &errInfo)
299302
msg := errInfo.Message
300-
if errInfo.Code != "custom" {
303+
if errInfo.Code != "" && errInfo.Code != "custom" {
301304
msg = fmt.Sprintf("%s.%s %s", errInfo.Resource, errInfo.Field, errorCodeToMessage(errInfo.Code))
302305
}
303306
if msg != "" {

api/client_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,20 @@ func TestRESTError(t *testing.T) {
129129

130130
}
131131
}
132+
133+
func TestHandleHTTPError_GraphQL502(t *testing.T) {
134+
req, err := http.NewRequest("GET", "https://api.github.com/user", nil)
135+
if err != nil {
136+
t.Fatal(err)
137+
}
138+
resp := &http.Response{
139+
Request: req,
140+
StatusCode: 502,
141+
Body: ioutil.NopCloser(bytes.NewBufferString(`{ "data": null, "errors": [{ "message": "Something went wrong" }] }`)),
142+
Header: map[string][]string{"Content-Type": {"application/json"}},
143+
}
144+
err = HandleHTTPError(resp)
145+
if err == nil || err.Error() != "HTTP 502: Something went wrong (https://api.github.com/user)" {
146+
t.Errorf("got error: %v", err)
147+
}
148+
}

0 commit comments

Comments
 (0)