From 8efde528a5308f94c00e7c5ae015311e00c6ec42 Mon Sep 17 00:00:00 2001 From: Gavriel Hirsch Date: Wed, 13 Apr 2022 13:37:06 -0500 Subject: [PATCH] fix README toy example --- README.md | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 3de1694..09f5eaf 100644 --- a/README.md +++ b/README.md @@ -49,14 +49,18 @@ call would block and retry with exponential backoff. It's possible for a request to succeed in the sense that the expected response headers are received, but then to encounter network-level errors while reading the response body. In go-retryablehttp's most basic usage, this error would not be retryable, due to the out-of-band handling of the response body. In some cases it may be desirable to handle the response body as part of the retryable operation. -In such a case, you can use `DoWithResponseHandler` (or `GetWithResponseHandler`) rather than `Do` (or `Get`). A toy example (which will retry the full request and succeed on the second attempt) is shown below: +A toy example (which will retry the full request and succeed on the second attempt) is shown below: ```go c := retryablehttp.NewClient() -handlerShouldRetry := false -c.GetWithResponseHandler("/foo", func(*http.Response) bool { - handlerShouldRetry = !handlerShouldRetry - return handlerShouldRetry +r := retryablehttp.NewRequest("GET", "://foo", nil) +handlerShouldRetry := true +r.SetResponseHandler(func(*http.Response) error { + if !handlerShouldRetry { + return nil + } + handlerShouldRetry = false + return errors.New("retryable error") }) ```