Skip to content

Commit e3c93cf

Browse files
committed
Merge pull request nfedyashev#6 from jondruse/master
add an exception callback option
2 parents 4136581 + 4502cc7 commit e3c93cf

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

lib/retryable.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
module Kernel
55
def retryable(options = {}, &block)
6-
opts = {:tries => 2, :sleep => 1, :on => StandardError, :matching => /.*/, :ensure => Proc.new {}}
6+
opts = {:tries => 2, :sleep => 1, :on => StandardError, :matching => /.*/, :ensure => Proc.new {}, :exception_cb => Proc.new {}}
77
check_for_invalid_options(options, opts)
88
opts.merge!(options)
99

@@ -28,6 +28,7 @@ def retryable(options = {}, &block)
2828

2929
retries += 1
3030
retry_exception = exception
31+
opts[:exception_cb].call(retry_exception)
3132
retry
3233
ensure
3334
opts[:ensure].call(retries)

spec/lib/retryable_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,12 @@
107107
retryable(:bad_option => 2) { raise "this is bad" }
108108
end.should raise_error ArgumentError, '[Retryable] Invalid options: bad_option'
109109
end
110+
111+
it 'accepts a callback to run after an exception is rescued' do
112+
lambda do
113+
retryable(:sleep => 0, :exception_cb => Proc.new {|e| @raised = e.to_s }) {|tries, ex| raise StandardError.new("this is fun!") if tries < 1 }
114+
end.should_not raise_error
115+
116+
@raised.should == "this is fun!"
117+
end
110118
end

0 commit comments

Comments
 (0)