Merge pull request #4154 from cli/graphql-502-error

Fix HTTP 502 error reporting from GraphQL request
This commit is contained in:
Mislav Marohnić 2021-08-23 12:03:34 +02:00 committed by GitHub
commit 4a45ca6fa6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 2 deletions

View file

@ -285,7 +285,10 @@ func HandleHTTPError(resp *http.Response) error {
return httpError
}
messages := []string{parsedBody.Message}
var messages []string
if parsedBody.Message != "" {
messages = append(messages, parsedBody.Message)
}
for _, raw := range parsedBody.Errors {
switch raw[0] {
case '"':
@ -297,7 +300,7 @@ func HandleHTTPError(resp *http.Response) error {
var errInfo HTTPErrorItem
_ = json.Unmarshal(raw, &errInfo)
msg := errInfo.Message
if errInfo.Code != "custom" {
if errInfo.Code != "" && errInfo.Code != "custom" {
msg = fmt.Sprintf("%s.%s %s", errInfo.Resource, errInfo.Field, errorCodeToMessage(errInfo.Code))
}
if msg != "" {

View file

@ -129,3 +129,20 @@ func TestRESTError(t *testing.T) {
}
}
func TestHandleHTTPError_GraphQL502(t *testing.T) {
req, err := http.NewRequest("GET", "https://api.github.com/user", nil)
if err != nil {
t.Fatal(err)
}
resp := &http.Response{
Request: req,
StatusCode: 502,
Body: ioutil.NopCloser(bytes.NewBufferString(`{ "data": null, "errors": [{ "message": "Something went wrong" }] }`)),
Header: map[string][]string{"Content-Type": {"application/json"}},
}
err = HandleHTTPError(resp)
if err == nil || err.Error() != "HTTP 502: Something went wrong (https://api.github.com/user)" {
t.Errorf("got error: %v", err)
}
}