How to catch error Connection reset by peer (Errno::ECONNRESET)
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
How can I get a more information than `Errno::ECONNRESET: An existing connection was forcibly closed by the remote host. - SSL_connect`
By setting RESTCLIENT_LOG=stdout
environment variable you will see RestClient debug data in the terminal. Or you can replace stdout with a file path to write it out to a log file.
The reason may be an invalid SSL cert or just a flaky API endpoint that sometimes disconnects before returning a response.
Related Topics
Use Ruby Array for a JavaScript Array in Erb. Escaping Quotes
Override "Show" Resource Route in Rails
Ruby on Rails Source Code Security/Obfuscation
Scanning for Unicode Numbers in a String with \D
-': Nil Can't Be Coerced into Fixnum (Typeerror)
Can't Get to Work Cocoapods and Yosemite
In Ruby, Why Is a Method Invocation Not Able to Be Treated as a Unit When "Do" and "End" Is Used
Ruby - Calling Setters from Within an Object
How to Handle Method Order in Ruby
Writing an Activerecord Adapter
In Ruby, What Are the Vertical Lines
Escaping Apostrophes Using Gsub
Is There Some Kind of Unseen Array Termination in Ruby
Cannot Load Such File -- MySQL2/2.4/Mysql2 (Loaderror) - Windows
How to Stop Rails' Built-In Server from Listening on 0.0.0.0 by Default