Skip to content
Snippets Groups Projects
Commit e775f167 authored by Hassanin Ahmed's avatar Hassanin Ahmed Committed by Thu Trang Pham
Browse files

handles unreachable network exception (#1030)

* handles unreachable network exception

* adds network error class

* tests unreachable host exception handling
parent 77fb2e47
No related branches found
No related tags found
No related merge requests found
...@@ -34,4 +34,7 @@ module Geocoder ...@@ -34,4 +34,7 @@ module Geocoder
class LookupTimeout < ::Timeout::Error class LookupTimeout < ::Timeout::Error
end end
class NetworkError < Error
end
end end
...@@ -286,6 +286,8 @@ module Geocoder ...@@ -286,6 +286,8 @@ module Geocoder
end end
rescue Timeout::Error rescue Timeout::Error
raise Geocoder::LookupTimeout raise Geocoder::LookupTimeout
rescue Errno::EHOSTUNREACH, Errno::ETIMEDOUT, Errno::ENETUNREACH
raise Geocoder::NetworkError
end end
def use_ssl? def use_ssl?
......
...@@ -129,6 +129,7 @@ module Geocoder ...@@ -129,6 +129,7 @@ module Geocoder
raise Timeout::Error if query.text == "timeout" raise Timeout::Error if query.text == "timeout"
raise SocketError if query.text == "socket_error" raise SocketError if query.text == "socket_error"
raise Errno::ECONNREFUSED if query.text == "connection_refused" raise Errno::ECONNREFUSED if query.text == "connection_refused"
raise Errno::EHOSTUNREACH if query.text == "host_unreachable"
if query.text == "invalid_json" if query.text == "invalid_json"
return MockHttpResponse.new(:body => 'invalid json', :code => 200) return MockHttpResponse.new(:body => 'invalid json', :code => 200)
end end
......
...@@ -75,4 +75,16 @@ class ErrorHandlingTest < GeocoderTestCase ...@@ -75,4 +75,16 @@ class ErrorHandlingTest < GeocoderTestCase
end end
end 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 end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment