我有一个类,它使用 savon 作为与 SOAP API 交互的 gem。有时服务器无法访问,因此实现soap方法的方法会抛出Timeout::Error
。实现这样的救援不是问题:
begin
...
rescue Timeour::Error
...
end
但是我定义了大约 50 个可能会受到影响的方法,并且我不想重复相同的代码 50 次。那绝对不干。有办法解决这个问题吗?我已经这样想过:
def safe_call method, params
begin
self.send method, params
rescue Timeour::Error
# do sth heroic to rescue the method
end
end
但这非常糟糕,因为我必须更改使用该类的所有脚本中的每个调用。有没有办法进行全类救援?
最佳答案
我认为你可以使用rescue_from。您可以在这里阅读:http://api.rubyonrails.org/classes/ActiveSupport/Rescuable/ClassMethods.html
简而言之,您可以将其放入应用程序 Controller 中:
rescue_from 'Timeout::Error' do |exception|
# Rescue logic here.
end
关于ruby-on-rails - 如何为 Timeout::error 定义类范围的救援方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10387107/