【问题标题】:How to know which exception was invoked如何知道调用了哪个异常
【发布时间】:2017-03-08 02:04:11
【问题描述】:

我正在编写一个 twitter 工具来收集一些数据。下面是sn-p的代码

      replies_without_root_tweet.each do |r|
        begin
          t = client.status(r.in_reply_to_status_id)
          RootTweet.find_or_create(t)
        rescue Twitter::Error::NotFound,Twitter::Error::Forbidden => e
          puts e
        end

现在的问题是,Twitter 搜索 API 有我经常遇到的速率限制。这里的问题是,如果我遇到此异常,如何在 15 分钟内恢复进程

Twitter::Error::TooManyRequests

正如您所看到的,我从另外两个异常中解救出来,如果我也添加了太多的请求异常,这将是一个问题,因为除非经过指定的时间,否则我可能会一直遇到该异常。

有没有办法知道特定异常何时触发,以便我可以让进程休眠?

【问题讨论】:

    标签: ruby-on-rails twitter


    【解决方案1】:

    您可以拥有任意数量的 rescue 语句,因此您可以这样做以不同于其他方式处理 TooManyRequests:

    begin
      t = client.status(r.in_reply_to_status_id)
      RootTweet.find_or_create(t)
    rescue Twitter::Error::NotFound, Twitter::Error::Forbidden => e
      puts e
    rescue Twitter::Error::TooManyRequests => e
      puts e
      sleep 10
    end
    

    您也可以询问错误对象它是什么,例如e.is_a?(Twitter::Error::TooManyRequests).

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-22
      • 1970-01-01
      • 2015-12-12
      • 1970-01-01
      • 1970-01-01
      • 2013-03-17
      • 2021-10-06
      • 2011-10-15
      相关资源
      最近更新 更多