Created
August 11, 2009 12:33
-
-
Save danlucraft/165785 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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