return response body as byte slice for RequestError type (#873)
This commit is contained in:
11
client.go
11
client.go
@@ -285,20 +285,21 @@ func (c *Client) baseURLWithAzureDeployment(baseURL, suffix, model string) (newB
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *Client) handleErrorResp(resp *http.Response) error {
|
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") {
|
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)
|
return fmt.Errorf("error, status code: %d, status: %s, body: %s", resp.StatusCode, resp.Status, body)
|
||||||
}
|
}
|
||||||
var errRes ErrorResponse
|
var errRes ErrorResponse
|
||||||
err := json.NewDecoder(resp.Body).Decode(&errRes)
|
err = json.Unmarshal(body, &errRes)
|
||||||
if err != nil || errRes.Error == nil {
|
if err != nil || errRes.Error == nil {
|
||||||
reqErr := &RequestError{
|
reqErr := &RequestError{
|
||||||
HTTPStatus: resp.Status,
|
HTTPStatus: resp.Status,
|
||||||
HTTPStatusCode: resp.StatusCode,
|
HTTPStatusCode: resp.StatusCode,
|
||||||
Err: err,
|
Err: err,
|
||||||
|
Body: body,
|
||||||
}
|
}
|
||||||
if errRes.Error != nil {
|
if errRes.Error != nil {
|
||||||
reqErr.Err = errRes.Error
|
reqErr.Err = errRes.Error
|
||||||
|
|||||||
Reference in New Issue
Block a user