问题
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