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

Adding a before callback to the .retry_on feature #48

Open
ryz310 opened this issue Jun 16, 2019 · 2 comments
Open

Adding a before callback to the .retry_on feature #48

ryz310 opened this issue Jun 16, 2019 · 2 comments
Assignees
Labels
enhancement New feature or request

Comments

@ryz310
Copy link
Owner

ryz310 commented Jun 16, 2019

For example, if an error occurs such as an access token expiring, it is useful to be able to regenerate the access token before retry processing. The current .retry_on feature is only useful in limited cases, but adding a before callback would be an enhancement.

@ryz310 ryz310 added the enhancement New feature or request label Jun 16, 2019
@ryz310 ryz310 self-assigned this Jun 16, 2019
@ryz310 ryz310 changed the title .retry_on enhancement Adding a before callback to the .retry_on feature Jun 16, 2019
@ryz310
Copy link
Owner Author

ryz310 commented Jun 18, 2019

I came up with following code. I think it would be worked as a before callback.

class ExampleApiClient < MyApiClient::Base
  endpoint 'https://example.com'

  # 1. Detects an error
  error_handling json: { '$.errors.code': 10 }, with: :access_token_expired

  # 3. Retry with updated access token
  retry_on AccessTokenExpired, wait: 1.second, attempts: 1

  private

  # 2. Refresh access token before raising exception
  def access_token_expired(params, logger)
    refresh_access_token!
    raise AccessTokenExpired, params
  end
end

@ryz310
Copy link
Owner Author

ryz310 commented Jun 25, 2019

#48 (comment)

Maybe it doesn't work as expected because it executes Exceptions#call in the Exceptions#call like as nested. I think it needs stack structure for arguments of #call calling.

def call(*args)
@args = args
send(*args)
rescue StandardError => e
@retry_count ||= 0
raise unless rescue_with_handler(e)
retry_result
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant