Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handling for non-json response #881

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,6 @@ func (c *Client) handleErrorResp(resp *http.Response) error {
if err != nil {
return fmt.Errorf("error, reading response body: %w", err)
}
if !strings.HasPrefix(resp.Header.Get("Content-Type"), "application/json") {
return fmt.Errorf("error, status code: %d, status: %s, body: %s", resp.StatusCode, resp.Status, body)
}
var errRes ErrorResponse
err = json.Unmarshal(body, &errRes)
if err != nil || errRes.Error == nil {
Expand Down
35 changes: 20 additions & 15 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,26 +194,31 @@ func TestHandleErrorResp(t *testing.T) {
{
"error":{}
}`)),
expected: "error, status code: 503, status: , message: ",
expected: `error, status code: 503, status: , message: , body:
{
"error":{}
}`,
},
{
name: "413 Request Entity Too Large",
httpCode: http.StatusRequestEntityTooLarge,
contentType: "text/html",
body: bytes.NewReader([]byte(`<html>
<head><title>413 Request Entity Too Large</title></head>
<body>
<center><h1>413 Request Entity Too Large</h1></center>
<hr><center>nginx</center>
</body>
</html>`)),
expected: `error, status code: 413, status: , body: <html>
<head><title>413 Request Entity Too Large</title></head>
<body>
<center><h1>413 Request Entity Too Large</h1></center>
<hr><center>nginx</center>
</body>
</html>`,
body: bytes.NewReader([]byte(`
<html>
<head><title>413 Request Entity Too Large</title></head>
<body>
<center><h1>413 Request Entity Too Large</h1></center>
<hr><center>nginx</center>
</body>
</html>`)),
expected: `error, status code: 413, status: , message: invalid character '<' looking for beginning of value, body:
<html>
<head><title>413 Request Entity Too Large</title></head>
<body>
<center><h1>413 Request Entity Too Large</h1></center>
<hr><center>nginx</center>
</body>
</html>`,
},
{
name: "errorReader",
Expand Down
5 changes: 4 additions & 1 deletion error.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ func (e *APIError) UnmarshalJSON(data []byte) (err error) {
}

func (e *RequestError) Error() string {
return fmt.Sprintf("error, status code: %d, status: %s, message: %s", e.HTTPStatusCode, e.HTTPStatus, e.Err)
return fmt.Sprintf(
"error, status code: %d, status: %s, message: %s, body: %s",
e.HTTPStatusCode, e.HTTPStatus, e.Err, e.Body,
)
}

func (e *RequestError) Unwrap() error {
Expand Down
Loading