From c9449a4f548af94345d8b5db94a91e83fe27f37a Mon Sep 17 00:00:00 2001
From: Alex Reisner <alex@alexreisner.com>
Date: Fri, 21 Dec 2012 14:49:43 -0500
Subject: [PATCH] Raise exception on Yandex API invalid key.

---
 lib/geocoder/lookups/yandex.rb |  6 +++++-
 test/services_test.rb          | 10 +++++++++-
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/lib/geocoder/lookups/yandex.rb b/lib/geocoder/lookups/yandex.rb
index 4b306e1e..272ecea9 100644
--- a/lib/geocoder/lookups/yandex.rb
+++ b/lib/geocoder/lookups/yandex.rb
@@ -21,7 +21,11 @@ module Geocoder::Lookup
     def results(query)
       return [] unless doc = fetch_data(query)
       if err = doc['error']
-        warn "Yandex Geocoding API error: #{err['status']} (#{err['message']})."
+        if err["status"] == 401 and err["message"] == "invalid key"
+          raise_error(Geocoder::InvalidApiKey) || warn("Invalid API key.")
+        else
+          warn "Yandex Geocoding API error: #{err['status']} (#{err['message']})."
+        end
         return []
       end
       if doc = doc['response']['GeoObjectCollection']
diff --git a/test/services_test.rb b/test/services_test.rb
index 3681c018..9c232a85 100644
--- a/test/services_test.rb
+++ b/test/services_test.rb
@@ -123,7 +123,7 @@ class ServicesTest < Test::Unit::TestCase
 
   # --- Yandex ---
 
-  def test_yandex_with_invalid_key
+  def test_yandex_warns_about_invalid_key
     # keep test output clean: suppress timeout warning
     orig = $VERBOSE; $VERBOSE = nil
     Geocoder.configure(:lookup => :yandex)
@@ -133,6 +133,14 @@ class ServicesTest < Test::Unit::TestCase
     $VERBOSE = orig
   end
 
+  def test_yandex_raises_exception_on_invalid_key
+    Geocoder.configure(:always_raise => [Geocoder::InvalidApiKey])
+    l = Geocoder::Lookup.get(:yandex)
+    assert_raises Geocoder::InvalidApiKey do
+      l.send(:results, Geocoder::Query.new("invalid key"))
+    end
+  end
+
 
   # --- Geocoder.ca ---
 
-- 
GitLab