Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

callback works if a request end with error #186

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

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