Skip to content
Browse files

It's better to raise named error (Geokit::TooManyQueriesError) for be…

…tter rescuing
  • Loading branch information...
1 parent 6cd67e2 commit f8f4f176b20fb14c2f4f0ddd73c45e46ac2fdbdb @drogus drogus committed Aug 1, 2009
Showing with 11 additions and 5 deletions.
  1. +10 −4 lib/geokit/geocoders.rb
  2. +1 −1 test/test_google_geocoder.rb
View
14 lib/geokit/geocoders.rb
@@ -5,6 +5,9 @@
require 'logger'
module Geokit
+
+ class TooManyQueriesError < StandardError; end
+
module Inflector
extend self
@@ -464,15 +467,18 @@ def self.xml2GeoLoc(xml, address="")
end
return geoloc
elsif doc.elements['//kml/Response/Status/code'].text == '620'
- raise "Google returned a 620 status, too many queries. The given key has gone over the requests limit in the 24 hour period or has submitted too many requests in too short a period of time. If you're sending multiple requests in parallel or in a tight loop, use a timer or pause in your code to make sure you don't send the requests too quickly."
+ raise Geokit::TooManyQueriesError
else
logger.info "Google was unable to geocode address: "+address
return GeoLoc.new
end
- rescue
- logger.error "Caught an error during Google geocoding call: "+$!
- return GeoLoc.new
+ rescue Geokit::TooManyQueriesError
+ # re-raise because of other rescue
+ raise Geokit::TooManyQueriesError, "Google returned a 620 status, too many queries. The given key has gone over the requests limit in the 24 hour period or has submitted too many requests in too short a period of time. If you're sending multiple requests in parallel or in a tight loop, use a timer or pause in your code to make sure you don't send the requests too quickly."
+ rescue
+ logger.error "Caught an error during Google geocoding call: "+$!
+ return GeoLoc.new
end
# extracts a single geoloc from a //placemark element in the google results xml
View
2 test/test_google_geocoder.rb
@@ -190,7 +190,7 @@ def test_too_many_queries
response.expects(:body).returns(GOOGLE_TOO_MANY)
url = "http://maps.google.com/maps/geo?q=#{Geokit::Inflector.url_escape(@address)}&output=xml&key=Google&oe=utf-8"
Geokit::Geocoders::GoogleGeocoder.expects(:call_geocoder_service).with(url).returns(response)
- assert_raise "" do
+ assert_raise Geokit::TooManyQueriesError do
res=Geokit::Geocoders::GoogleGeocoder.geocode(@address)
end
end

0 comments on commit f8f4f17

Please sign in to comment.
Something went wrong with that request. Please try again.