Skip to content

Commit cfe15ff

Browse files
return response body as byte slice for RequestError type (sashabaranov#873)
1 parent 9913264 commit cfe15ff

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

client.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -285,20 +285,21 @@ func (c *Client) baseURLWithAzureDeployment(baseURL, suffix, model string) (newB
285285
}
286286

287287
func (c *Client) handleErrorResp(resp *http.Response) error {
288+
body, err := io.ReadAll(resp.Body)
289+
if err != nil {
290+
return fmt.Errorf("error, reading response body: %w", err)
291+
}
288292
if !strings.HasPrefix(resp.Header.Get("Content-Type"), "application/json") {
289-
body, err := io.ReadAll(resp.Body)
290-
if err != nil {
291-
return fmt.Errorf("error, reading response body: %w", err)
292-
}
293293
return fmt.Errorf("error, status code: %d, status: %s, body: %s", resp.StatusCode, resp.Status, body)
294294
}
295295
var errRes ErrorResponse
296-
err := json.NewDecoder(resp.Body).Decode(&errRes)
296+
err = json.Unmarshal(body, &errRes)
297297
if err != nil || errRes.Error == nil {
298298
reqErr := &RequestError{
299299
HTTPStatus: resp.Status,
300300
HTTPStatusCode: resp.StatusCode,
301301
Err: err,
302+
Body: body,
302303
}
303304
if errRes.Error != nil {
304305
reqErr.Err = errRes.Error

error.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type RequestError struct {
2929
HTTPStatus string
3030
HTTPStatusCode int
3131
Err error
32+
Body []byte
3233
}
3334

3435
type ErrorResponse struct {

0 commit comments

Comments
 (0)