ActiveSupport Rescuable - 可以重试吗?

时间:2016-08-25 08:05:15

标签: ruby-on-rails ruby activesupport

我正在使用ActiveSupport的可恢复性。是retry引发异常的代码是可能的吗?

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

重试抛出

  

无效重试(SyntaxError)

(我猜是因为重试不是直接在救援区?)

有办法吗?

1 个答案:

答案 0 :(得分:1)

正如文档here所述,您必须在rescue区块中重试:

例如,代码为:

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

所以你只需实现方法should_retry来决定何时应该重试:)

相关问题