I’m using Rails 4.2.7 and this code to get a webpage through a SOCKS proxy …
begin
…
res1 = Net::HTTP.SOCKSProxy('127.0.0.1', 50001).start(uri.host, uri.port) do |http|
puts "launching #{uri}"
resp = http.get(uri)
status = resp.code
content = resp.body
content_type = resp['content-type']
content_encoding = resp['content-encoding']
end
…
rescue OpenURI::HTTPError => ex
…
rescue SocketError, Net::OpenTimeout => e
…
Occasionally, I get an error on the line “rest = http.get(uri)”
Error during processing: buffer error
/Users/mikeb/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/net/http/response.rb:364:in `finish'
/Users/mikeb/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/net/http/response.rb:364:in `finish'
/Users/mikeb/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/net/http/response.rb:266:in `ensure in inflater'
/Users/mikeb/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/net/http/response.rb:265:in `inflater'
/Users/mikeb/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/net/http/response.rb:281:in `read_body_0'
/Users/mikeb/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/net/http/response.rb:202:in `read_body'
/Users/mikeb/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/net/http.rb:1157:in `block in get'
/Users/mikeb/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/net/http.rb:1446:in `block in transport_request'
/Users/mikeb/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/net/http/response.rb:163:in `reading_body'
/Users/mikeb/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/net/http.rb:1445:in `transport_request'
/Users/mikeb/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/net/http.rb:1407:in `request'
/Users/mikeb/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/net/http.rb:1156:in `get'
/Users/mikeb/Documents/workspace/runtrax/app/helpers/webpage_helper.rb:99:in `block in get_content'
How do I catch this error? You see the types of errors I’m catching above but I don’t know how to catch the particular error I’m getting. My intention is to retry if such an error occurs.
There's always
The error is being thrown from
@inflate.finish
, where@inflate = Zlib::Inflate.new(32 + Zlib::MAX_WBITS)
. You may have to require zlib to explicitly catch the error, see: How can I catch Zlib::BufError in my Ruby on Rails code?