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)
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.
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.
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)
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With