From 7511dc9a131b93c6017e80b507c2d29f6cc92d98 Mon Sep 17 00:00:00 2001
From: Alex Reisner <alex@alexreisner.com>
Date: Thu, 12 Jun 2014 09:27:43 -0400
Subject: [PATCH] Rescue from Errno::ECONNREFUSED.

Response to GH issues #579 and #663.
---
 lib/geocoder/lookups/base.rb     |  2 ++
 test/test_helper.rb              |  1 +
 test/unit/error_handling_test.rb | 12 ++++++++++++
 3 files changed, 15 insertions(+)

diff --git a/lib/geocoder/lookups/base.rb b/lib/geocoder/lookups/base.rb
index cb41337b..22a7c349 100644
--- a/lib/geocoder/lookups/base.rb
+++ b/lib/geocoder/lookups/base.rb
@@ -169,6 +169,8 @@ module Geocoder
         parse_raw_data fetch_raw_data(query)
       rescue SocketError => err
         raise_error(err) or warn "Geocoding API connection cannot be established."
+      rescue Errno::ECONNREFUSED => err
+        raise_error(err) or warn "Geocoding API connection refused."
       rescue TimeoutError => err
         raise_error(err) or warn "Geocoding API not responding fast enough " +
           "(use Geocoder.configure(:timeout => ...) to set limit)."
diff --git a/test/test_helper.rb b/test/test_helper.rb
index 2309827b..d872af3d 100644
--- a/test/test_helper.rb
+++ b/test/test_helper.rb
@@ -110,6 +110,7 @@ module Geocoder
       def make_api_request(query)
         raise TimeoutError if query.text == "timeout"
         raise SocketError if query.text == "socket_error"
+        raise Errno::ECONNREFUSED if query.text == "connection_refused"
         read_fixture fixture_for_query(query)
       end
     end
diff --git a/test/unit/error_handling_test.rb b/test/unit/error_handling_test.rb
index e999af94..29f2ccbb 100644
--- a/test/unit/error_handling_test.rb
+++ b/test/unit/error_handling_test.rb
@@ -41,4 +41,16 @@ class ErrorHandlingTest < GeocoderTestCase
       end
     end
   end
+
+  def test_always_raise_connection_refused_error
+    Geocoder.configure(:always_raise => [Errno::ECONNREFUSED])
+    Geocoder::Lookup.all_services_except_test.each do |l|
+      next if l == :maxmind_local # local, does not raise timeout
+      lookup = Geocoder::Lookup.get(l)
+      set_api_key!(l)
+      assert_raises Errno::ECONNREFUSED do
+        lookup.send(:results, Geocoder::Query.new("connection_refused"))
+      end
+    end
+  end
 end
-- 
GitLab