diff --git a/src/xutil/xhttp/request_test.go b/src/xutil/xhttp/request_test.go index 41576fe..754d8ef 100644 --- a/src/xutil/xhttp/request_test.go +++ b/src/xutil/xhttp/request_test.go @@ -40,7 +40,7 @@ func TestRequestError(t *testing.T) { a.NotNil(err) } -func TestDebugAndRetry(t *testing.T) { +func TestDebugAndRetryFail(t *testing.T) { a := assert.New(t) count := 0 @@ -67,7 +67,29 @@ func TestDebugAndRetry(t *testing.T) { a.Equal(count, 2) } -func TestDebugAndAbortRetry(t *testing.T) { +func TestDebugAndRetrySuccess(t *testing.T) { + a := assert.New(t) + + count := 0 + xhttp.DefaultOptions.DebugFunc = func(l *xhttp.Log) { + log.Printf("%+v %+v %+v %+v\n", l.Duration, l.Request, l.Response, l.Error) + count++ + } + + url := "https://aaaaa.com/" + retryIf := func(resp *xhttp.XResponse, err error) error { + if count == 1 { + return errors.New("the first request failed") + } + return nil + } + _, err := xhttp.Request("GET", url, xhttp.WithRetry(retryIf, retry.Attempts(3))) + + a.Nil(err) + a.Equal(count, 2) +} + +func TestDebugAndRetryAbort(t *testing.T) { a := assert.New(t) count := 0 diff --git a/src/xutil/xhttp/retry.go b/src/xutil/xhttp/retry.go index 340e333..a1a8f57 100644 --- a/src/xutil/xhttp/retry.go +++ b/src/xutil/xhttp/retry.go @@ -20,6 +20,15 @@ func (t Error) Error() string { return errors.Join(logWithNumber...).Error() } +func (t Error) HasAbortRetry() bool { + for _, err := range t { + if errors.Is(err, ErrAbortRetry) { + return true + } + } + return false +} + func doRetry(opts *requestOptions, f func() (*XResponse, error)) (*XResponse, error) { var resp *XResponse var err error @@ -47,7 +56,7 @@ func doRetry(opts *requestOptions, f func() (*XResponse, error)) (*XResponse, er if err != nil { return nil, err } - if len(errorLog) > 0 { + if errorLog.HasAbortRetry() { return nil, &errorLog } return resp, nil