Skip to content

Commit c865543

Browse files
authored
fix: return error including the actual error message (extracted from response body) (#6)
1 parent 85c4409 commit c865543

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

client.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -259,13 +259,14 @@ func sendRequestStream[T streamable](client *Client, req *http.Request, retryOpt
259259
}
260260

261261
// handle status codes
262-
failures = append(failures, fmt.Sprintf("#%d/%d error response received: %v", i+1, options.Retries+1, client.handleErrorResp(resp)))
262+
errResp := client.handleErrorResp(resp)
263+
failures = append(failures, fmt.Sprintf("#%d/%d error response received: %v", i+1, options.Retries+1, errResp))
263264

264265
// exit on non-retriable status codes
265266
if !options.canRetry(resp.StatusCode) {
266267
failures = append(failures, fmt.Sprintf("exiting due to non-retriable error in try #%d/%d: %d %s", i+1, options.Retries+1, resp.StatusCode, resp.Status))
267268
slog.Error("sendRequestStream failed due to non-retriable statuscode", "code", resp.StatusCode, "status", resp.Status, "tries", i+1, "maxTries", options.Retries+1, "failures", strings.Join(failures, "; "))
268-
return nil, fmt.Errorf("request failed on non-retriable status-code: %d %s", resp.StatusCode, resp.Status)
269+
return nil, fmt.Errorf("request failed on non-retriable status-code: %s", resp.StatusCode, errResp.Error())
269270
}
270271

271272
// exponential backoff
@@ -358,11 +359,13 @@ func (c *Client) handleErrorResp(resp *http.Response) error {
358359
err := json.NewDecoder(bytes.NewBuffer(data)).Decode(&errRes)
359360
if err == nil && errRes.Error != nil && errRes.Error.Message != "" {
360361
errRes.Error.HTTPStatusCode = resp.StatusCode
362+
errRes.Error.HTTPStatus = resp.Status
361363
return errRes.Error
362364
}
363365

364366
return &RequestError{
365367
HTTPStatusCode: resp.StatusCode,
368+
HTTPStatus: resp.Status,
366369
Err: errors.New(string(data)),
367370
}
368371
}

error.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type APIError struct {
1414
Param *string `json:"param,omitempty"`
1515
Type string `json:"type"`
1616
HTTPStatusCode int `json:"-"`
17+
HTTPStatus string `json:"-"`
1718
InnerError *InnerError `json:"innererror,omitempty"`
1819
}
1920

@@ -26,6 +27,7 @@ type InnerError struct {
2627
// RequestError provides informations about generic request errors.
2728
type RequestError struct {
2829
HTTPStatusCode int
30+
HTTPStatus string
2931
Err error
3032
}
3133

@@ -35,7 +37,7 @@ type ErrorResponse struct {
3537

3638
func (e *APIError) Error() string {
3739
if e.HTTPStatusCode > 0 {
38-
return fmt.Sprintf("error, status code: %d, message: %s", e.HTTPStatusCode, e.Message)
40+
return fmt.Sprintf("error, status code: %d (%s), message: %s", e.HTTPStatusCode, e.HTTPStatus, e.Message)
3941
}
4042

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

103105
func (e *RequestError) Error() string {
104-
return fmt.Sprintf("error, status code: %d, message: %s", e.HTTPStatusCode, e.Err)
106+
return fmt.Sprintf("error, status code: %d (%s), message: %s", e.HTTPStatusCode, e.HTTPStatus, e.Err)
105107
}
106108

107109
func (e *RequestError) Unwrap() error {

0 commit comments

Comments
 (0)