Skip to content

Commit

Permalink
only apply regex when http status code available
Browse files Browse the repository at this point in the history
Signed-off-by: Chengxuan Xing <chengxuan.xing@kaleido.io>
  • Loading branch information
Chengxuan committed Sep 14, 2023
1 parent 045c504 commit 3da3888
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/ffresty/ffresty.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func NewWithConfig(ctx context.Context, ffrestyConfig Config) (client *resty.Cli
return false
}

if retryStatusCodeRegex != nil && !retryStatusCodeRegex.MatchString(r.Status()) {
if r.StatusCode() > 0 && retryStatusCodeRegex != nil && !retryStatusCodeRegex.MatchString(r.Status()) {
// the error status code doesn't match the retry status code regex, stop retry
return false
}
Expand Down
12 changes: 12 additions & 0 deletions pkg/ffresty/ffresty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"crypto/x509/pkix"
"encoding/json"
"encoding/pem"
"errors"
"fmt"
"log"
"math/big"
Expand Down Expand Up @@ -135,6 +136,9 @@ func TestRequestRetryErrorStatusCodeRegex(t *testing.T) {
httpmock.RegisterResponder("GET", "http://localhost:12345/test2",
httpmock.NewStringResponder(429, `{"message": "pop"}`))

httpmock.RegisterResponder("GET", "http://localhost:12345/test3",
httpmock.NewErrorResponder(errors.New("not http response")))

resp, err := c.R().Get("/test")
assert.NoError(t, err)
assert.Equal(t, 500, resp.StatusCode())
Expand All @@ -151,6 +155,14 @@ func TestRequestRetryErrorStatusCodeRegex(t *testing.T) {
err = WrapRestErr(ctx, resp, err, i18n.MsgConfigFailed)
assert.Error(t, err)

resp, err = c.R().Get("/test3")
assert.Error(t, err)
assert.Equal(t, 0, resp.StatusCode())
assert.Equal(t, 13, httpmock.GetTotalCallCount())

err = WrapRestErr(ctx, resp, err, i18n.MsgConfigFailed)
assert.Error(t, err)

}
func TestConfWithProxy(t *testing.T) {

Expand Down

0 comments on commit 3da3888

Please sign in to comment.