|
7 | 7 | "io" |
8 | 8 | "io/ioutil" |
9 | 9 | "net/http" |
| 10 | + "net/url" |
10 | 11 | "regexp" |
11 | 12 | "strings" |
12 | 13 |
|
@@ -154,18 +155,21 @@ func (gr GraphQLErrorResponse) Error() string { |
154 | 155 | for _, e := range gr.Errors { |
155 | 156 | errorMessages = append(errorMessages, e.Message) |
156 | 157 | } |
157 | | - return fmt.Sprintf("graphql error: '%s'", strings.Join(errorMessages, ", ")) |
| 158 | + return fmt.Sprintf("GraphQL error: %s", strings.Join(errorMessages, "\n")) |
158 | 159 | } |
159 | 160 |
|
160 | 161 | // HTTPError is an error returned by a failed API call |
161 | 162 | type HTTPError struct { |
162 | | - Code int |
163 | | - URL string |
164 | | - Message string |
| 163 | + StatusCode int |
| 164 | + RequestURL *url.URL |
| 165 | + Message string |
165 | 166 | } |
166 | 167 |
|
167 | | -func (e HTTPError) Error() string { |
168 | | - return fmt.Sprintf("http error, '%s' failed (%d): '%s'", e.URL, e.Code, e.Message) |
| 168 | +func (err HTTPError) Error() string { |
| 169 | + if err.Message != "" { |
| 170 | + return fmt.Sprintf("HTTP %d: %s (%s)", err.StatusCode, err.Message, err.RequestURL) |
| 171 | + } |
| 172 | + return fmt.Sprintf("HTTP %d (%s)", err.StatusCode, err.RequestURL) |
169 | 173 | } |
170 | 174 |
|
171 | 175 | // Returns whether or not scopes are present, appID, and error |
@@ -294,26 +298,25 @@ func handleResponse(resp *http.Response, data interface{}) error { |
294 | 298 | } |
295 | 299 |
|
296 | 300 | func handleHTTPError(resp *http.Response) error { |
297 | | - var message string |
298 | | - var parsedBody struct { |
299 | | - Message string `json:"message"` |
| 301 | + httpError := HTTPError{ |
| 302 | + StatusCode: resp.StatusCode, |
| 303 | + RequestURL: resp.Request.URL, |
300 | 304 | } |
| 305 | + |
301 | 306 | body, err := ioutil.ReadAll(resp.Body) |
302 | 307 | if err != nil { |
303 | | - return err |
304 | | - } |
305 | | - err = json.Unmarshal(body, &parsedBody) |
306 | | - if err != nil { |
307 | | - message = string(body) |
308 | | - } else { |
309 | | - message = parsedBody.Message |
| 308 | + httpError.Message = err.Error() |
| 309 | + return httpError |
310 | 310 | } |
311 | 311 |
|
312 | | - return HTTPError{ |
313 | | - Code: resp.StatusCode, |
314 | | - URL: resp.Request.URL.String(), |
315 | | - Message: message, |
| 312 | + var parsedBody struct { |
| 313 | + Message string `json:"message"` |
| 314 | + } |
| 315 | + if err := json.Unmarshal(body, &parsedBody); err == nil { |
| 316 | + httpError.Message = parsedBody.Message |
316 | 317 | } |
| 318 | + |
| 319 | + return httpError |
317 | 320 | } |
318 | 321 |
|
319 | 322 | var jsonTypeRE = regexp.MustCompile(`[/+]json($|;)`) |
|
0 commit comments