簡體   English   中英

在Ruby中,您可以為Twitter創建救援程序,以便在發生錯誤時繼續進行循環嗎?

[英]In Ruby, can you create a rescue for twitter for when there is an error it will continue the loop?

我正在嘗試創建一種rescue ,如果出現Twitter::Error::NotFound錯誤(例如不存在)時,它將繼續循環。 請幫忙,謝謝。

下面是代碼,

begin
  File.open("user_ids.txt") do |file| 
    file.each do |id|
      puts client.user("#{id}").screen_name
    rescue Twitter::Error::NotFound => error
      next # skip this item
    end
  end
end

除了retry方法外,還有一種方法可以跳過並繼續前進到循環中的下一個項目? 我很確定error.rate_limit不適用(我從另一個救援調用中復制了此代碼),是否有另一種調用方法? error.notfound.continue_with_loop

我想創建一個挽救工具,如果並且當出現錯誤(例如does not exist ,它將繼續循環。 請幫忙,謝謝。

是的next將繼續並在循環中重試下一個項目。 retry將使用相同的項目重試循環。

注意:對於該方法中的所有do ,您沒有足夠的end 所以我會嘗試:

begin
  File.open("user_ids.txt") do |file| 
    file.each do |id|
      puts client.user("#{id}").screen_name
    rescue Twitter::Error::NotFound => error
      sleep error.rate_limit.reset_in + 1
      next # skip this item
    end
  end
end

注:見縮進如何正確清楚,當你缺少end

您可能需要將當前很多的開始/結束塊轉移到您要從中搶救的代碼周圍(否則它將默認為外部開始/結束而不是循環)

File.open("user_ids.txt") do |file| 
  file.each do |id|
    begin
      puts client.user("#{id}").screen_name
    rescue Twitter::Error::NotFound => error
      sleep error.rate_limit.reset_in + 1
      next # skip this item
    end
  end
end

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM