How can I catch Zlib::BufError in my Ruby on Rails code?

纵然是瞬间 提交于 2019-12-13 05:53:20

问题


I have built an app with Rails. I have a large list of addresses which I am standardizing via the Bing/Google API using Geocoder. This is my current call to get the adddress info:

Geocoder.search(address)

This works perfectly fine and I can parse the response JSON. However when I leave the script running it sometimes falls over with the following error:

Zlib::BufError: buffer error

Instead of the script falling over I would like to catch this error and continue with the script. How could I do this with Ruby? I am not sure which exception to catch, if any.


回答1:


To ensure Zlib is available, you need to require 'zlib' in the file where you want to catch the error. It doesn't look like Geocoder has declared that as a dependency itself anywhere, so it may not be loaded when your rescue line is being evaluated.

require 'zlib'

begin
  Geocoder.search('110 William St, 28th Floor New York, NY 10038')
rescue Zlib::BufError => boom
  puts 'Stack Overflow appears to have moved.'
end


来源:https://stackoverflow.com/questions/32870168/how-can-i-catch-zlibbuferror-in-my-ruby-on-rails-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!