Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: return error including the actual error message (extracted from response body) #6

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,13 +259,14 @@ func sendRequestStream[T streamable](client *Client, req *http.Request, retryOpt
}

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

// exit on non-retriable status codes
if !options.canRetry(resp.StatusCode) {
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))
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, "; "))
return nil, fmt.Errorf("request failed on non-retriable status-code: %d %s", resp.StatusCode, resp.Status)
return nil, fmt.Errorf("request failed on non-retriable status-code: %s", resp.StatusCode, errResp.Error())
}

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

return &RequestError{
HTTPStatusCode: resp.StatusCode,
HTTPStatus: resp.Status,
Err: errors.New(string(data)),
}
}
Expand Down
6 changes: 4 additions & 2 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type APIError struct {
Param *string `json:"param,omitempty"`
Type string `json:"type"`
HTTPStatusCode int `json:"-"`
HTTPStatus string `json:"-"`
InnerError *InnerError `json:"innererror,omitempty"`
}

Expand All @@ -26,6 +27,7 @@ type InnerError struct {
// RequestError provides informations about generic request errors.
type RequestError struct {
HTTPStatusCode int
HTTPStatus string
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 (%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 (%s), message: %s", e.HTTPStatusCode, e.HTTPStatus, e.Err)
}

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