Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 1 addition & 7 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,7 @@ func TestHandleErrorResp(t *testing.T) {
<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>`,
expected: `error, status code: 413, status: , message: invalid character '<' looking for beginning of value`,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This expected error text is a bit misleading. Code 413 indeed points out to "Content Too Large" (generic HTTP error), but the text afterward applies to json de-serialization.

I agree that this approach is better in terms of preserving the code, but in this case we actually don't preserve data

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if Error() for RequestError (https://github.com/sashabaranov/go-openai/blob/22cf414b4be89a38eb702b13c27be1de6bcef74d/error.go#L106C1-L108C2) is updated to return body also like:
fmt.Sprintf("error, status code: %d, status: %s, message: %s, body: %s", e.HTTPStatusCode, e.HTTPStatus, e.Err, e.Body)

Should I make this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sashabaranov I have updated this PR with the above mentioned change, now in error response the original error data is also present. Can you please review it?

},
{
name: "errorReader",
Expand Down