【发布时间】:2025-12-04 21:50:01
【问题描述】:
我正在尝试找出在 Ruby on Rails 中捕获特定错误和错误消息的最佳方法。我的用例是我时不时遇到一个超时错误,该错误会引发一般错误,我希望将超时错误与相同一般错误中的其他错误区别对待。我不确定一般错误中可能会引发哪些其他类型的错误,但我认为存在更多错误。我在下面有一些示例代码说明我目前的处理方式,但我想可能有更好的方法我还没有找到?
tries = 0
begin
tries += 1
<code>
rescue Foo::Bar => e
case e.to_s
when 'More specific timeout error message'
retry unless tries >= 5
else
# Let me see other error messages
log.info("Error: #{e.to_s}")
end
end
【问题讨论】:
-
您确定超时是使用通用异常类而不是其自己的类引发的,例如 Timeout::Error?
-
是的,所以错误来自一个外部库,它不时超时,并返回外部库一般错误和超时消息。
标签: ruby-on-rails ruby error-handling rescue