Merge pull request #4154 from cli/graphql-502-error
Fix HTTP 502 error reporting from GraphQL request
This commit is contained in:
commit
4a45ca6fa6
2 changed files with 22 additions and 2 deletions
|
|
@ -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 != "" {
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue