Skip to content

Commit

Permalink
Handling for non-json response (#881)
Browse files Browse the repository at this point in the history
* removed handling for non-json response

* added response body in RequestError.Error() and updated tests

* done linting
  • Loading branch information
AyushSawant18588 authored Oct 21, 2024
1 parent 9fe2c6c commit fb15ff9
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 19 deletions.
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

0 comments on commit fb15ff9

Please sign in to comment.