3

我正在使用 Twitter Gem,并且创建了一个长时间运行的 ruby​​ 任务。我希望它能够处理常见错误,所以我正在寻找一个我应该考虑防止的列表(例如失败的鲸鱼 500)

这是我的代码功能的开始/结束循环:

Begin

# My (omitted) very long ruby task
# filled with Twitter API requests

rescue Errno::ENOENT
  sleep(5)
  logger.info "ENOENT error - attempting to retry"
  retry
rescue Errno::ETIMEDOUT
  sleep(5)
  logger.info " Operation timed out - attempting to retry"
  retry
rescue Errno::ECONNRESET
  sleep(5)
  logger.info "Connection reset by peer - attempting to retry"
  retry
end 

您能想到要保护和重试的任何其他错误吗?这是一种处理错误的结构良好的方法吗?我应该考虑哪些设计实现?

4

3 回答 3

2

考虑在最后设置一个包罗万象的异常处理程序,记录遇到的异常类型并重新引发它。您的脚本第一次可能会失败,但至少您会找出原因。

begin

# My (omitted) very long ruby task
# filled with Twitter API requests

rescue Errno::ENOENT
  sleep(5)
  logger.info "ENOENT error - attempting to retry"
  retry
rescue Errno::ETIMEDOUT
  sleep(5)
  logger.info " Operation timed out - attempting to retry"
  retry
rescue Errno::ECONNRESET
  sleep(5)
  logger.info "Connection reset by peer - attempting to retry"
  retry
rescue # This rescues StandardError and its children
  sleep(5)
  # The next line is somewhat pseudocode, because I don't use logger
  logger.this_is_somewhat_bad "Somewhat bad exception #{$!.class} #{$!} happened - I'm giving up"
  raise
rescue Exception
  sleep(5)
  # The next line is somewhat pseudocode, because I don't use logger
  logger.omg_wtf_bbq "Really bad exception #{$!.class} #{$!} happened - I'm giving up"
  raise
end
于 2011-06-20T23:01:00.667 回答
0

我还在Twitter::Forbidden使用 Twitter gem 的代码中发现了错误。

于 2011-06-20T18:33:03.977 回答
0

或者,您可以尝试rescue SystemCallError,因为所有Errno错误都是它的子类。

于 2012-03-14T16:19:47.953 回答