Skip to content

Instantly share code, notes, and snippets.

@danlucraft
Created August 11, 2009 12:33
Show Gist options
  • Save danlucraft/165785 to your computer and use it in GitHub Desktop.
Save danlucraft/165785 to your computer and use it in GitHub Desktop.
module Songkick
module Retryable
# A simple helper to allow a block to be retried a given
# number of times, but only if the correct type of error was
# raised.
#
# @param [Integer] the number of times to retry. The block will never be run more times than this.
# @param [Class] the type of errors to retry on.
# @param [Regexp] the error message must match this to retry.
# @return the return value of the block.
def retry_(times, error_type=Exception, message=nil)
i = 0
begin
i += 1
yield
rescue error_type => e
if message
if e.message =~ message
retry unless i == times
else
raise e
end
else
retry unless i == times
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment