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

Return the body alongside the status in the baseRetryPolicy in case of error #212

Open
adrianlungu opened this issue Dec 23, 2023 · 3 comments

Comments

@adrianlungu
Copy link

Presently, when the following case is true inside the baseRetryPolicy:

if resp.StatusCode == 0 || (resp.StatusCode >= 500 && resp.StatusCode != http.StatusNotImplemented) {

the only things returned are the status and the statusCode, however, in some cases, the server returns more information regarding the error within the response's body.

Adding the response body to the error returned by the baseRetryPolicy would be helpful for logging and debugging purposes.

@FFCMSouza
Copy link

FFCMSouza commented Jan 23, 2024

I found a way to return the response body by setting the errorHandler like this:

client := retryablehttp.NewClient()
client.ErrorHandler = errorHandler

func errorHandler(resp *http.Response, err error, attempt int) (retresp *http.Response, reterr error) {
	if err != nil {
		return nil, fmt.Errorf("%s %s giving up after %d attempt(s): %w",
			resp.Request.Method, resp.Request.URL, attempt, err)
	}
	localVarBody, _ := io.ReadAll(resp.Body)
	resp.Body.Close()
	errmsg := fmt.Sprintf("%s %s", resp.Status, string(localVarBody[:]))
	return nil, fmt.Errorf("%s %s giving up after %d attempt(s): %s",
		resp.Request.Method, resp.Request.URL, attempt, errmsg)
}

@adrianlungu
Copy link
Author

adrianlungu commented Feb 19, 2024

@FFCMSouza I think there may be a small typo over there as the first check should be that err == nil based on what the library code does if the errorHandler is not specified; i.e.

func errorHandler(resp *http.Response, err error, attempt int) (*http.Response, error) {
	var req http.Request
	var bodyStr string

	if resp != nil && resp.Request != nil {
		req = *resp.Request

		body, readErr := io.ReadAll(resp.Body)
		defer resp.Body.Close()
		if readErr != nil {
			err = errors.Wrap(err, fmt.Sprintf("failed to read response body: %v", readErr))
		} else {
			bodyStr = string(body)
		}
	}

	// this means CheckRetry thought the request was a failure, but didn't communicate why
	if err == nil {
		return resp, fmt.Errorf("%s %s giving up after %d attempt(s): %s",
			req.Method, req.URL, attempt, bodyStr)
	}

	return resp, fmt.Errorf("%s %s giving up after %d attempt(s) with error %w: %s",
		req.Method, req.URL, attempt, err, bodyStr)
}

@FFCMSouza
Copy link

Hi @adrianlungu.
There is no typo. In my case I just need to return the body when there is no previous error on the request or check retry. When there is an error, I just return that error.
If error is null then I return the body.
The order of conditions makes no difference to the result.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants