Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch error Connection reset by peer (Errno::ECONNRESET)

Tags:

ruby

The following code sometimes generates a "connection reset by peer" error. Can anyone show me how to handle this exception?

doc = Nokogiri::HTML(open(url))
Connection reset by peer (Errno::ECONNRESET)
like image 403
revolver Avatar asked Feb 09 '12 03:02

revolver


People also ask

What does connection reset by peer error mean?

This error is generated when the OS receives notification of TCP Reset (RST) from the remote peer. Connection reset by peer means the TCP stream was abnormally closed from the other end. A TCP RST was received and the connection is now closed.

What is TCP reset by peer?

This error is generated when the OS receives notification of TCP Reset (RST) from the remote peer. Connection reset by peer means the TCP stream was abnormally closed from the other end.

What does the windows connection reset by peer (wsaconnreset) mean?

Please note: You have to REDUCE the value, not increase it. The Windows Connection Reset by Peer (WSACONNRESET or error number 10053) indicates that the connection to a communication partner was broken for UNKOWN reasons. network connection lost (cable unplugged, switch down, WLAN device shutdown)

What does the windows connection reset by peer error number 10053 mean?

The Windows Connection Reset by Peer (WSACONNRESET or error number 10053) indicates that the connection to a communication partner was broken for UNKOWN reasons. Help to improve this answer by adding a comment. If you have a different answer for this question, then please use the Your Answer form at the bottom of the page instead.


1 Answers

To catch it, do it just like any other exception:

begin
  doc = Nokogiri::HTML(open(url))
rescue Errno::ECONNRESET => e
  puts "we are handling it!"
end

A more useful pattern is to try a couple of times, then give up:

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
like image 51
Daniel Pittman Avatar answered Oct 21 '22 14:10

Daniel Pittman