lib/tins/attempt.rb in tins-1.31.1 vs lib/tins/attempt.rb in tins-1.32.0
- old
+ new
@@ -12,10 +12,11 @@
# Iff *reraise* is true the caught exception is reraised after running out
# of attempts.
def attempt(opts = {}, &block)
sleep = nil
exception_class = StandardError
+ prev_exception = nil
if Numeric === opts
attempts = opts
else
attempts = opts[:attempts] || 1
attempts >= 1 or raise ArgumentError, 'at least one attempt is required'
@@ -26,23 +27,24 @@
return if attempts <= 0
count = 0
if exception_class.nil?
begin
count += 1
- if block.call(count)
+ if block.call(count, prev_exception)
return true
elsif count < attempts
sleep_duration(sleep, count)
end
end until count == attempts
false
else
begin
count += 1
- block.call(count)
+ block.call(count, prev_exception)
true
rescue *exception_class
if count < attempts
+ prev_exception = $!
sleep_duration(sleep, count)
retry
end
case reraise
when Proc