我似乎在重用相同rescue
的 s. 有没有可能有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