以下代码有时会产生“由对等连接重置”错误.任何人都可以告诉我如何处理这个异常?
doc = Nokogiri::HTML(open(url)) Connection reset by peer (Errno::ECONNRESET)
解决方法
要抓住它,就像任何其他例外一样:
begin doc = Nokogiri::HTML(open(url)) rescue Errno::ECONNRESET => e puts "we are handling it!" end
一个更有用的模式是尝试几次,然后放弃:
count = 0 begin doc = Nokogiri::HTML(open(url)) rescue Errno::ECONNRESET => e count += 1 retry unless count > 10 puts "tried 10 times and couldn't get #{url}: #{e} end