Skip to content

Conversation

@achillesss
Copy link

@achillesss achillesss commented Apr 11, 2018

NewRequest().End(callbackFun)

if an error occurs in a request, the func will return without calling the callback function in which i could have handled the error:

for {
 		resp, body, errs = s.getResponseBytes()
 		if errs != nil {
                         // here returns
			return nil, nil, errs
 		}
                 ...
}

so we may change the return to break, and return the result at the bottom of the whole function like this:

func (s *SuperAgent) EndBytes(callback ...func(response Response, body []byte, errs []error)) (Response, []byte, []error) {
	var (
		errs         []error
		resp         Response
		body         []byte
		respCallback http.Response
	)

	for {
		resp, body, errs = s.getResponseBytes()
		if errs != nil {
			break
		}

		if s.isRetryableRequest(resp) {
			resp.Header.Set("Retry-Count", strconv.Itoa(s.Retryable.Attempt))
			respCallback = *resp
			break
		}
	}

	if len(callback) != 0 {
		callback[0](&respCallback, body, s.Errors)
	}
	return resp, body, nil
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant