Handling for non-json response (#881)

* removed handling for non-json response

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

* done linting
This commit is contained in:
Ayush Sawant
2024-10-22 02:19:34 +05:30
committed by GitHub
parent 9fe2c6ce1f
commit fb15ff9dcd
3 changed files with 24 additions and 19 deletions

View File

@@ -289,9 +289,6 @@ func (c *Client) handleErrorResp(resp *http.Response) error {
if err != nil { if err != nil {
return fmt.Errorf("error, reading response body: %w", err) 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 var errRes ErrorResponse
err = json.Unmarshal(body, &errRes) err = json.Unmarshal(body, &errRes)
if err != nil || errRes.Error == nil { if err != nil || errRes.Error == nil {

View File

@@ -194,26 +194,31 @@ func TestHandleErrorResp(t *testing.T) {
{ {
"error":{} "error":{}
}`)), }`)),
expected: "error, status code: 503, status: , message: ", expected: `error, status code: 503, status: , message: , body:
{
"error":{}
}`,
}, },
{ {
name: "413 Request Entity Too Large", name: "413 Request Entity Too Large",
httpCode: http.StatusRequestEntityTooLarge, httpCode: http.StatusRequestEntityTooLarge,
contentType: "text/html", contentType: "text/html",
body: bytes.NewReader([]byte(`<html> body: bytes.NewReader([]byte(`
<head><title>413 Request Entity Too Large</title></head> <html>
<body> <head><title>413 Request Entity Too Large</title></head>
<center><h1>413 Request Entity Too Large</h1></center> <body>
<hr><center>nginx</center> <center><h1>413 Request Entity Too Large</h1></center>
</body> <hr><center>nginx</center>
</html>`)), </body>
expected: `error, status code: 413, status: , body: <html> </html>`)),
<head><title>413 Request Entity Too Large</title></head> expected: `error, status code: 413, status: , message: invalid character '<' looking for beginning of value, body:
<body> <html>
<center><h1>413 Request Entity Too Large</h1></center> <head><title>413 Request Entity Too Large</title></head>
<hr><center>nginx</center> <body>
</body> <center><h1>413 Request Entity Too Large</h1></center>
</html>`, <hr><center>nginx</center>
</body>
</html>`,
}, },
{ {
name: "errorReader", name: "errorReader",

View File

@@ -104,7 +104,10 @@ func (e *APIError) UnmarshalJSON(data []byte) (err error) {
} }
func (e *RequestError) Error() string { 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 { func (e *RequestError) Unwrap() error {