Skip to content

Commit

Permalink
Call error block on retries
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuatobin committed Oct 15, 2014
1 parent bddc3f8 commit 0d10532
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
20 changes: 12 additions & 8 deletions lib/relish.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,23 +99,27 @@ def delete(id, version)
end
end

def set_error_handler(&block)
@error_handler = block
def set_error_handler(&blk)
@error_handler = blk
end

def rescue_dynamodb_error
tries = @tries
begin
yield
rescue => e
if (tries -= 1).zero?
raise
else
unless @error_handler.nil?
@error_handler.call(e)
end
retries = ((tries -= 1) > 0)

unless @error_handler.nil?
@error_handler.call(e, retries)
end

if retries
retry
else
raise
end

end
end
end
10 changes: 7 additions & 3 deletions spec/relish_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,19 @@
assert_requested(:post, @dynamo_url, times: 3)
end

it "calls a custom proc so consumers can log/measure Dynamo errors" do
it "calls a custom proc so consumers can log/measure Dynamo errors with retries" do
@error = nil
@relish.set_error_handler { |e| @error = e }
@retries = []
@relish.set_error_handler do |e, r|
@error = e
@retries << r
end
assert_raise Excon::Errors::ServiceUnavailable do
@relish.copy("1234", "1", { name: "foobar" })
end
assert_equal Excon::Errors::ServiceUnavailable, @error.class
assert_equal [true, true, false], @retries
end
end

end
end

0 comments on commit 0d10532

Please sign in to comment.