From e775f167c8e6222fc7ef56c8798765c1071d3002 Mon Sep 17 00:00:00 2001 From: Hassanin Ahmed <sas1ni69@gmail.com> Date: Tue, 7 Jun 2016 21:19:20 +0800 Subject: [PATCH] handles unreachable network exception (#1030) * handles unreachable network exception * adds network error class * tests unreachable host exception handling --- lib/geocoder/exceptions.rb | 3 +++ lib/geocoder/lookups/base.rb | 2 ++ test/test_helper.rb | 1 + test/unit/error_handling_test.rb | 12 ++++++++++++ 4 files changed, 18 insertions(+) diff --git a/lib/geocoder/exceptions.rb b/lib/geocoder/exceptions.rb index 280b9dd0..228c45b3 100644 --- a/lib/geocoder/exceptions.rb +++ b/lib/geocoder/exceptions.rb @@ -34,4 +34,7 @@ module Geocoder class LookupTimeout < ::Timeout::Error end + class NetworkError < Error + end + end diff --git a/lib/geocoder/lookups/base.rb b/lib/geocoder/lookups/base.rb index ed9b946b..e1db3980 100644 --- a/lib/geocoder/lookups/base.rb +++ b/lib/geocoder/lookups/base.rb @@ -286,6 +286,8 @@ module Geocoder end rescue Timeout::Error raise Geocoder::LookupTimeout + rescue Errno::EHOSTUNREACH, Errno::ETIMEDOUT, Errno::ENETUNREACH + raise Geocoder::NetworkError end def use_ssl? diff --git a/test/test_helper.rb b/test/test_helper.rb index 00055c80..89aa2bef 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -129,6 +129,7 @@ module Geocoder raise Timeout::Error if query.text == "timeout" raise SocketError if query.text == "socket_error" raise Errno::ECONNREFUSED if query.text == "connection_refused" + raise Errno::EHOSTUNREACH if query.text == "host_unreachable" if query.text == "invalid_json" return MockHttpResponse.new(:body => 'invalid json', :code => 200) end diff --git a/test/unit/error_handling_test.rb b/test/unit/error_handling_test.rb index 2052b033..ca20f3e5 100644 --- a/test/unit/error_handling_test.rb +++ b/test/unit/error_handling_test.rb @@ -75,4 +75,16 @@ class ErrorHandlingTest < GeocoderTestCase end end end + + def test_always_raise_host_unreachable_error + Geocoder.configure(:always_raise => [Errno::EHOSTUNREACH]) + Geocoder::Lookup.all_services_except_test.each do |l| + next if l == :maxmind_local || l == :geoip2 # local, does not use cache + lookup = Geocoder::Lookup.get(l) + set_api_key!(l) + assert_raises Errno::EHOSTUNREACH do + lookup.send(:results, Geocoder::Query.new("host_unreachable")) + end + end + end end -- GitLab