Include status code in UnexpectedHTTPResponseError

Signed-off-by: Aaron Lehmann <aaron.lehmann@docker.com>
This commit is contained in:
Aaron Lehmann 2016-03-15 09:03:56 -07:00
parent cd6482ecb8
commit 3a2231fe39
2 changed files with 12 additions and 9 deletions

View file

@ -29,11 +29,12 @@ func (e *UnexpectedHTTPStatusError) Error() string {
// is returned, but the content was unexpected and failed to be parsed. // is returned, but the content was unexpected and failed to be parsed.
type UnexpectedHTTPResponseError struct { type UnexpectedHTTPResponseError struct {
ParseErr error ParseErr error
StatusCode int
Response []byte Response []byte
} }
func (e *UnexpectedHTTPResponseError) Error() string { func (e *UnexpectedHTTPResponseError) Error() string {
return fmt.Sprintf("error parsing HTTP response: %s: %q", e.ParseErr.Error(), string(e.Response)) return fmt.Sprintf("error parsing HTTP %d response body: %s: %q", e.StatusCode, e.ParseErr.Error(), string(e.Response))
} }
func parseHTTPErrorResponse(statusCode int, r io.Reader) error { func parseHTTPErrorResponse(statusCode int, r io.Reader) error {
@ -59,6 +60,7 @@ func parseHTTPErrorResponse(statusCode int, r io.Reader) error {
if err := json.Unmarshal(body, &errors); err != nil { if err := json.Unmarshal(body, &errors); err != nil {
return &UnexpectedHTTPResponseError{ return &UnexpectedHTTPResponseError{
ParseErr: err, ParseErr: err,
StatusCode: statusCode,
Response: body, Response: body,
} }
} }
@ -68,6 +70,7 @@ func parseHTTPErrorResponse(statusCode int, r io.Reader) error {
// UnexpectedHTTPResponseError. // UnexpectedHTTPResponseError.
return &UnexpectedHTTPResponseError{ return &UnexpectedHTTPResponseError{
ParseErr: ErrNoErrorsInBody, ParseErr: ErrNoErrorsInBody,
StatusCode: statusCode,
Response: body, Response: body,
} }
} }

View file

@ -68,7 +68,7 @@ func TestHandleErrorResponseExpectedStatusCode404EmptyErrorSlice(t *testing.T) {
} }
err := HandleErrorResponse(response) err := HandleErrorResponse(response)
expectedMsg := `error parsing HTTP response: no error details found in HTTP response body: "{\"randomkey\": \"randomvalue\"}"` expectedMsg := `error parsing HTTP 404 response body: no error details found in HTTP response body: "{\"randomkey\": \"randomvalue\"}"`
if !strings.Contains(err.Error(), expectedMsg) { if !strings.Contains(err.Error(), expectedMsg) {
t.Errorf("Expected \"%s\", got: \"%s\"", expectedMsg, err.Error()) t.Errorf("Expected \"%s\", got: \"%s\"", expectedMsg, err.Error())
} }
@ -83,7 +83,7 @@ func TestHandleErrorResponseExpectedStatusCode404InvalidBody(t *testing.T) {
} }
err := HandleErrorResponse(response) err := HandleErrorResponse(response)
expectedMsg := "error parsing HTTP response: invalid character 'i' looking for beginning of object key string: \"{invalid json}\"" expectedMsg := "error parsing HTTP 404 response body: invalid character 'i' looking for beginning of object key string: \"{invalid json}\""
if !strings.Contains(err.Error(), expectedMsg) { if !strings.Contains(err.Error(), expectedMsg) {
t.Errorf("Expected \"%s\", got: \"%s\"", expectedMsg, err.Error()) t.Errorf("Expected \"%s\", got: \"%s\"", expectedMsg, err.Error())
} }