Skip to content

Commit fb15ff9

Browse files
Handling for non-json response (sashabaranov#881)
* removed handling for non-json response * added response body in RequestError.Error() and updated tests * done linting
1 parent 9fe2c6c commit fb15ff9

File tree

3 files changed

+24
-19
lines changed

3 files changed

+24
-19
lines changed

client.go

-3
Original file line numberDiff line numberDiff line change
@@ -289,9 +289,6 @@ func (c *Client) handleErrorResp(resp *http.Response) error {
289289
if err != nil {
290290
return fmt.Errorf("error, reading response body: %w", err)
291291
}
292-
if !strings.HasPrefix(resp.Header.Get("Content-Type"), "application/json") {
293-
return fmt.Errorf("error, status code: %d, status: %s, body: %s", resp.StatusCode, resp.Status, body)
294-
}
295292
var errRes ErrorResponse
296293
err = json.Unmarshal(body, &errRes)
297294
if err != nil || errRes.Error == nil {

client_test.go

+20-15
Original file line numberDiff line numberDiff line change
@@ -194,26 +194,31 @@ func TestHandleErrorResp(t *testing.T) {
194194
{
195195
"error":{}
196196
}`)),
197-
expected: "error, status code: 503, status: , message: ",
197+
expected: `error, status code: 503, status: , message: , body:
198+
{
199+
"error":{}
200+
}`,
198201
},
199202
{
200203
name: "413 Request Entity Too Large",
201204
httpCode: http.StatusRequestEntityTooLarge,
202205
contentType: "text/html",
203-
body: bytes.NewReader([]byte(`<html>
204-
<head><title>413 Request Entity Too Large</title></head>
205-
<body>
206-
<center><h1>413 Request Entity Too Large</h1></center>
207-
<hr><center>nginx</center>
208-
</body>
209-
</html>`)),
210-
expected: `error, status code: 413, status: , body: <html>
211-
<head><title>413 Request Entity Too Large</title></head>
212-
<body>
213-
<center><h1>413 Request Entity Too Large</h1></center>
214-
<hr><center>nginx</center>
215-
</body>
216-
</html>`,
206+
body: bytes.NewReader([]byte(`
207+
<html>
208+
<head><title>413 Request Entity Too Large</title></head>
209+
<body>
210+
<center><h1>413 Request Entity Too Large</h1></center>
211+
<hr><center>nginx</center>
212+
</body>
213+
</html>`)),
214+
expected: `error, status code: 413, status: , message: invalid character '<' looking for beginning of value, body:
215+
<html>
216+
<head><title>413 Request Entity Too Large</title></head>
217+
<body>
218+
<center><h1>413 Request Entity Too Large</h1></center>
219+
<hr><center>nginx</center>
220+
</body>
221+
</html>`,
217222
},
218223
{
219224
name: "errorReader",

error.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,10 @@ func (e *APIError) UnmarshalJSON(data []byte) (err error) {
104104
}
105105

106106
func (e *RequestError) Error() string {
107-
return fmt.Sprintf("error, status code: %d, status: %s, message: %s", e.HTTPStatusCode, e.HTTPStatus, e.Err)
107+
return fmt.Sprintf(
108+
"error, status code: %d, status: %s, message: %s, body: %s",
109+
e.HTTPStatusCode, e.HTTPStatus, e.Err, e.Body,
110+
)
108111
}
109112

110113
func (e *RequestError) Unwrap() error {

0 commit comments

Comments
 (0)