Optimize Client Error Return (#856)

* update client error return

* update client_test.go

* update client_test.go

* update file_api_test.go

* update client_test.go

* update client_test.go
This commit is contained in:
eiixy
2024-09-26 18:25:56 +08:00
committed by GitHub
parent e095df5325
commit 38bdc812df
4 changed files with 67 additions and 25 deletions

View File

@@ -285,10 +285,18 @@ func (c *Client) baseURLWithAzureDeployment(baseURL, suffix, model string) (newB
}
func (c *Client) handleErrorResp(resp *http.Response) error {
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)
if err != nil || errRes.Error == nil {
reqErr := &RequestError{
HTTPStatus: resp.Status,
HTTPStatusCode: resp.StatusCode,
Err: err,
}
@@ -298,6 +306,7 @@ func (c *Client) handleErrorResp(resp *http.Response) error {
return reqErr
}
errRes.Error.HTTPStatus = resp.Status
errRes.Error.HTTPStatusCode = resp.StatusCode
return errRes.Error
}