Skip to content

Commit

Permalink
return response body as byte slice for RequestError type (#873)
Browse files Browse the repository at this point in the history
  • Loading branch information
AyushSawant18588 authored Oct 14, 2024
1 parent 9913264 commit cfe15ff
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
11 changes: 6 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type RequestError struct {
HTTPStatus string
HTTPStatusCode int
Err error
Body []byte
}

type ErrorResponse struct {
Expand Down

0 comments on commit cfe15ff

Please sign in to comment.