Twitter Gem - 需要考虑救援吗?
我正在使用 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
你可以吗想到任何其他需要保护和重试的错误吗?这是处理错误的结构良好的方法吗?我应该考虑哪些设计实现?
I'm working with the Twitter Gem and I've created a long running ruby task. I would like it to be able to handle common errors so I'm looking to build a list of those I should consider to protect against (for example the fail whale 500)
Here is the begin/end loop my code functions in:
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
Can you think of any other Errors to protect and retry against? Is this a well structured way to handle errors? What are some design implementations I should consider?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
考虑在最后有一个捕获所有异常的处理程序,记录遇到的异常类型并重新引发它。您的脚本第一次可能会失败,但至少您会找出原因。
Consider having a catch-all exception handler at the end that logs what kind of exception was encountered and re-raises it. Your script may fail the first time, but at least you'll find out why.
我还在使用 Twitter gem 的代码中发现了
Twitter::Forbidden
错误。I also catch the
Twitter::Forbidden
error in my code that uses the Twitter gem.或者,您可以尝试
rescue SystemCallError
,因为所有Errno
错误都是该错误的子类。Alternatively, you could try
rescue SystemCallError
, since allErrno
errors are subclasses of that.