Skip to content
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
13 changes: 13 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,19 @@ func (c *Client) AddRetryCondition(condition RetryConditionFunc) *Client {
return c
}

// AddRetryAfterErrorCondition adds the basic condition of retrying after encountering
// an error from the http response
func (c *Client) AddRetryAfterErrorCondition() *Client {
c.AddRetryCondition(func(response *Response, err error) bool {
if response.IsError() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gtpaulose can you please add a test case for success and failure flow of new condition?
More info: https://codecov.io/gh/go-resty/resty/pull/384/diff?src=pr&el=tree#diff-Y2xpZW50Lmdv

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey did you mean the test case for 'refiring under an error and non error scenario (i.e response.IsError() == true and response.IsError() == false )? '

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

Copy link
Contributor Author

@gtpaulose gtpaulose Nov 2, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, ive added the test case. Ive reused an existing path in the createGetServer() switch case, hope thats okay!

return true
}

return false
})
return c
}

// SetTLSClientConfig method sets TLSClientConfig for underling client Transport.
//
// For Example:
Expand Down
9 changes: 9 additions & 0 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,15 @@ func TestClientOptions(t *testing.T) {
client.SetRetryMaxWaitTime(mrwt)
assertEqual(t, mrwt, client.RetryMaxWaitTime)

client.AddRetryAfterErrorCondition()
equal(client.RetryConditions[0], func(response *Response, err error) bool {
if response.IsError() {
return true
}

return false
})

err := &AuthError{}
client.SetError(err)
if reflect.TypeOf(err) == client.Error {
Expand Down
25 changes: 25 additions & 0 deletions retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,31 @@ func TestClientRetryCount(t *testing.T) {
strings.HasPrefix(err.Error(), "Get \""+ts.URL+"/set-retrycount-test\"")))
}

func TestClientErrorRetry(t *testing.T) {
ts := createGetServer(t)
defer ts.Close()

c := dc().
SetTimeout(time.Second * 3).
SetRetryCount(1).
AddRetryAfterErrorCondition()

resp, err := c.R().
SetHeader(hdrContentTypeKey, "application/json; charset=utf-8").
SetJSONEscapeHTML(false).
SetResult(AuthSuccess{}).
Get(ts.URL + "/set-retry-error-recover")

assertError(t, err)

authSuccess := resp.Result().(*AuthSuccess)

assertEqual(t, http.StatusOK, resp.StatusCode())
assertEqual(t, "hello", authSuccess.Message)

assertNil(t, resp.Error())
}

func filler(*Response, error) bool {
return false
}