Skip to content

Instantly share code, notes, and snippets.

@krames
Last active August 29, 2015 14:04
Show Gist options
  • Save krames/7dd4c390178adbc9840a to your computer and use it in GitHub Desktop.
Save krames/7dd4c390178adbc9840a to your computer and use it in GitHub Desktop.
def request(verb, endpoint, opts={})
retry_on_auth_failure do
cached_token.request(verb, endpoint, opts)
end
end
def retry_on_auth_failure(retries=1, timeout=0, &block)
attempts_remaining = retries
begin
yield
rescue OAuth2::Error => error
if error.response.status == 401 && attempts_remaining > 0
attempts_remaining -= 1
cache_new_token
sleep(timeout)
retry
else
raise error
end
end
end
@arv25
Copy link

arv25 commented Aug 6, 2014

What about just this:

  def request(verb, endpoint, opts={})
    remaining_retries ||= 1
    cached_token.request(verb, endpoint, opts)
  rescue OAuth2::Error => error
    if retry?(error, remaining_retries)
      cache_new_token
      remaining_retries -= 1
      retry
    else
      raise error
    end
  end

Reference: http://blog.mirthlab.com/2012/05/25/cleanly-retrying-blocks-of-code-after-an-exception-in-ruby/

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