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:
@@ -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 {
|
||||
|
||||
@@ -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",
|
||||
|
||||
5
error.go
5
error.go
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user