同じを再利用しているようですrescue
。rescue
ブロックを持つことは可能でしょうか?したがって、代わりに:
while count != 0 do
<<code>>
rescue error1
<<code>>
retry
rescue error2
<<code>>
break
end
私は持てます:
def rescue_block
rescue error 1
<<code>>
retry
rescue error 2
<<code>>
break
end
while count !=0
<<code>>
rescue_block
end
とに注意しbreak
てくださいretry
。アクションはrescue_block
、ループ自体ではなく、ループに適用する必要があります。
編集:
Twitter gemを使用すると、Twitterのエラーのほとんどを同じ方法で処理できます(つまり、APIの制限に達した場合は、待機します)。完全なコード:
while cursor != 0 do
begin
grabFollowers ? f = Twitter.follower_ids(username,{:cursor=>cursor}) : f = Twitter.friend_ids(username,{:cursor=>cursor})
outfile.puts(f.ids) if writeHumanReadable
arrayOfIds << f.ids
cursor = f.next_cursor
rescue Twitter::Error::Unauthorized
break
rescue Twitter::Error::BadRequest
#ran out of OAUTH calls, waits until OAUTH reset + 10 seconds.
outErrorFile = File.new("#{username.to_s}-ERROR.txt",'w')
resetTime = Twitter.rate_limit_status.reset_time_in_seconds
current = Time.now.to_i
pauseDuration = resetTime-current+10
outErrorFile.puts(Time.now)
outErrorFile.puts(pauseDuration/60)
outErrorFile.close
sleep(pauseDuration)
retry
rescue Twitter::Error::NotFound
break
rescue Twitter::Error::ServiceUnavailable
sleep(60*5)
retry
end
sleep(2)
end