From 9caa8d3fc230b476f2055473d8e7cd20142f7596 Mon Sep 17 00:00:00 2001 From: ayush Date: Thu, 10 Oct 2024 08:45:08 +0000 Subject: [PATCH] return response body as byte slice for RequestError type --- client.go | 11 ++++++----- error.go | 1 + 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/client.go b/client.go index 583244fe1..1e228a097 100644 --- a/client.go +++ b/client.go @@ -285,20 +285,21 @@ func (c *Client) baseURLWithAzureDeployment(baseURL, suffix, model string) (newB } func (c *Client) handleErrorResp(resp *http.Response) error { + body, err := io.ReadAll(resp.Body) + if err != nil { + return fmt.Errorf("error, reading response body: %w", err) + } if !strings.HasPrefix(resp.Header.Get("Content-Type"), "application/json") { - body, err := io.ReadAll(resp.Body) - if err != nil { - return fmt.Errorf("error, reading response body: %w", err) - } return fmt.Errorf("error, status code: %d, status: %s, body: %s", resp.StatusCode, resp.Status, body) } var errRes ErrorResponse - err := json.NewDecoder(resp.Body).Decode(&errRes) + err = json.Unmarshal(body, &errRes) if err != nil || errRes.Error == nil { reqErr := &RequestError{ HTTPStatus: resp.Status, HTTPStatusCode: resp.StatusCode, Err: err, + Body: body, } if errRes.Error != nil { reqErr.Err = errRes.Error diff --git a/error.go b/error.go index 1f6a8971d..fc9e7cdb9 100644 --- a/error.go +++ b/error.go @@ -29,6 +29,7 @@ type RequestError struct { HTTPStatus string HTTPStatusCode int Err error + Body []byte } type ErrorResponse struct {