How do I catch the “Error during processing: buffe

2019-07-21 07:47发布

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.

1条回答
唯我独甜
2楼-- · 2019-07-21 08:30

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?

查看更多
登录 后发表回答