@@ -1006,29 +1006,38 @@ func (s *SuperAgent) EndBytes(callback ...func(response Response, body []byte, e
10061006
10071007 for {
10081008 resp , body , errs = s .getResponseBytes ()
1009- if errs != nil {
1010- return nil , nil , errs
1011- }
1012- if s .isRetryableRequest (resp ) {
1013- resp .Header .Set ("Retry-Count" , strconv .Itoa (s .Retryable .Attempt ))
1009+ // if errs != nil {
1010+ // return nil, nil, errs
1011+ // }
1012+ if ! s .shouldRetry (resp , len (errs ) > 0 ) {
1013+ if resp != nil {
1014+ resp .Header .Set ("Retry-Count" , strconv .Itoa (s .Retryable .Attempt ))
1015+ }
10141016 break
10151017 }
1018+
1019+ s .Errors = nil
10161020 }
10171021
1018- respCallback := * resp
10191022 if len (callback ) != 0 {
1020- callback [0 ](& respCallback , body , s .Errors )
1023+ if resp == nil {
1024+ callback [0 ](nil , body , s .Errors )
1025+ } else {
1026+ respCallback := * resp
1027+ callback [0 ](& respCallback , body , s .Errors )
1028+ }
1029+
10211030 }
1022- return resp , body , nil
1031+ return resp , body , errs
10231032}
10241033
1025- func (s * SuperAgent ) isRetryableRequest (resp Response ) bool {
1026- if s .Retryable .Enable && s .Retryable .Attempt < s .Retryable .RetryerCount && statusesContains (s .Retryable .RetryableStatus , resp .StatusCode ) {
1034+ func (s * SuperAgent ) shouldRetry (resp Response , hasError bool ) bool {
1035+ if s .Retryable .Enable && s .Retryable .Attempt < s .Retryable .RetryerCount && ( hasError || statusesContains (s .Retryable .RetryableStatus , resp .StatusCode ) ) {
10271036 time .Sleep (s .Retryable .RetryerTime )
10281037 s .Retryable .Attempt ++
1029- return false
1038+ return true
10301039 }
1031- return true
1040+ return false
10321041}
10331042
10341043// EndStruct should be used when you want the body as a struct. The callbacks work the same way as with `End`, except that a struct is used instead of a string.
0 commit comments