Cyril Duchon-Doris Cyril Duchon-Doris - 3 months ago 8
Ruby Question

ActiveSupport Rescuable - is it possible to retry?

I am using ActiveSupport's rescuable. is it possible to

retry
the code that raised the exception ?

rescue SomeException, with: :my_handler

def my_code
...
rescue => exception
rescue_with_handler(exception) || raise
end

def my_handler
if ...
retry
else
raise
end
end


The retry throws a


Invalid retry (SyntaxError)


(I guess because the retry isn't in a rescue block directly ?)

Is there a way around it ?

Answer

As the document said here, you have to call retry in your rescue block:

For instance, the code would be:

def my_code
  ...
rescue => exception
  should_retry(exception) ? retry : raise
end

So you just implement the method should_retry to decide when should do retry :)