2

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.

  • Possibly helpful, or not Zlib: http://stackoverflow.com/questions/32870168/how-can-i-catch-zlibbuferror-in-my-ruby-on-rails-code – Elvn Oct 11 '16 at 16:33
  • There's always `rescue StandardError` and `retry`: http://stackoverflow.com/a/39984645/1298944 – Scott Bartell Oct 12 '16 at 05:38
  • @ValAsenio, I'm not grasping how your link helps me. I'm not using ZLib. –  Oct 12 '16 at 14:34

1 Answers1

1

There's always

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
…
rescue Zlib:BufError => e # 

end

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?

Community
  • 1
  • 1
Patrick McGuire
  • 329
  • 1
  • 12
  • 1
    Rescue StandardError instead. Its a [very bad idea to rescue Exception](https://stackoverflow.com/questions/10048173/why-is-it-bad-style-to-rescue-exception-e-in-ruby). – infused Oct 11 '16 at 23:14
  • I had `rescue => e` rather than `rescue StandardError => e`. Even though they do the same thing I updated it to make it clearer. – Patrick McGuire Oct 11 '16 at 23:27
  • Does "StandardError" only catch the error I mention in my question, or will it catch any error? Note taht I don't want to catch any error -- I only want to deal with the error mentinoed in my question. –  Oct 12 '16 at 13:46
  • Ok, the link at the top looks correct. http://stackoverflow.com/questions/32870168/how-can-i-catch-zlibbuferror-in-my-ruby-on-rails-code It does look like the error is Zlib::BufError, you can catch that. May have to require, I'll update my answer. – Patrick McGuire Oct 12 '16 at 15:53