Skip to content

Commit

Permalink
update client error return
Browse files Browse the repository at this point in the history
  • Loading branch information
eiixy committed Sep 20, 2024
1 parent a5fb553 commit 1133adc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
9 changes: 9 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 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,
}
Expand All @@ -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
}
Expand Down
6 changes: 4 additions & 2 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type APIError struct {
Message string `json:"message"`
Param *string `json:"param,omitempty"`
Type string `json:"type"`
HTTPStatus string `json:"-"`
HTTPStatusCode int `json:"-"`
InnerError *InnerError `json:"innererror,omitempty"`
}
Expand All @@ -25,6 +26,7 @@ type InnerError struct {

// RequestError provides information about generic request errors.
type RequestError struct {
HTTPStatus string
HTTPStatusCode int
Err error
}
Expand All @@ -35,7 +37,7 @@ type ErrorResponse struct {

func (e *APIError) Error() string {
if e.HTTPStatusCode > 0 {
return fmt.Sprintf("error, status code: %d, message: %s", e.HTTPStatusCode, e.Message)
return fmt.Sprintf("error, status code: %d, status: %s, message: %s", e.HTTPStatusCode, e.HTTPStatus, e.Message)
}

return e.Message
Expand Down Expand Up @@ -101,7 +103,7 @@ func (e *APIError) UnmarshalJSON(data []byte) (err error) {
}

func (e *RequestError) Error() string {
return fmt.Sprintf("error, status code: %d, message: %s", e.HTTPStatusCode, e.Err)
return fmt.Sprintf("error, status code: %d, status: %s, message: %s", e.HTTPStatusCode, e.HTTPStatus, e.Err)
}

func (e *RequestError) Unwrap() error {
Expand Down

0 comments on commit 1133adc

Please sign in to comment.