0

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.

Todd A. Jacobs
  • 81,402
  • 15
  • 141
  • 199
Arthur
  • 1,970
  • 4
  • 18
  • 19
  • What do you mean, you don't know the exception? `Zlib::BufError` is the exception. – Sergio Tulentsev Sep 30 '15 at 15:37
  • I have tried rescue Zlib::BufError and it keeps telling me it is an unknown error. I thought this would too but was getting that unknown error. I will try again and look into why that could be. Thanks for the reply. – Arthur Sep 30 '15 at 15:42
  • what do you mean, "unknown error"? – Sergio Tulentsev Sep 30 '15 at 15:45
  • I do not have the exact message but it was along the lines of "undefined exception zlib:buferror" I could have typo'd something. I have added the begin rescue back in and will see what happens. – Arthur Sep 30 '15 at 15:47
  • There's something amiss here. This is a report of a `Z_BUF_ERROR` being returned by zlib. What it means is that that particular call of `inflate()` or `deflate()` could not make any progress due to not having any input or not having any output space. This error is not fatal. You can always continue by providing more input or more output space. So something should try to keep going and you should not get this error, unless it is being used to indicate something else. E.g. an incomplete zlib stream being decoded. – Mark Adler Oct 01 '15 at 15:18
  • Yea. I am pulling JSON from the bing geocode API and every so often it was throwing that error, maybe a time out or not getting all the data, my internet connection is very flaky. As mentioned before I was trying to rescue the ZLib::BufError but I needed to include zlib for some reason. Answer below. – Arthur Oct 01 '15 at 17:08

1 Answers1

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
Kristján
  • 18,165
  • 5
  • 50
  • 62