Skip to content
Snippets Groups Projects
Commit 8e9a4252 authored by Ahmed Adam's avatar Ahmed Adam Committed by Alex Reisner
Browse files

Add option to not rescue from certain exceptions.

Allows custom handling of geocoding service connection problems.
parent aa0f1cbd
No related branches found
No related tags found
No related merge requests found
...@@ -28,7 +28,12 @@ module Geocoder ...@@ -28,7 +28,12 @@ module Geocoder
[:cache, nil], [:cache, nil],
# prefix (string) to use for all cache keys # prefix (string) to use for all cache keys
[:cache_prefix, "geocoder:"] [:cache_prefix, "geocoder:"],
# exceptions that should not be rescued by default
# (if you want to implement custom error handling);
# supports SocketError and TimeoutError
[:always_raise, []]
] ]
end end
......
...@@ -90,16 +90,24 @@ module Geocoder ...@@ -90,16 +90,24 @@ module Geocoder
eval("Geocoder::Result::#{self.class.to_s.split(":").last}") eval("Geocoder::Result::#{self.class.to_s.split(":").last}")
end end
##
# Raise exception instead of warning for specified exceptions.
#
def raise_error(err)
raise err if Geocoder::Configuration.always_raise.include?(err.class)
end
## ##
# Returns a parsed search result (Ruby hash). # Returns a parsed search result (Ruby hash).
# #
def fetch_data(query, reverse = false) def fetch_data(query, reverse = false)
begin begin
parse_raw_data fetch_raw_data(query, reverse) parse_raw_data fetch_raw_data(query, reverse)
rescue SocketError rescue SocketError => err
warn "Geocoding API connection cannot be established." raise_error(err) or warn "Geocoding API connection cannot be established."
rescue TimeoutError rescue TimeoutError => err
warn "Geocoding API not responding fast enough " + raise_error(err) or warn "Geocoding API not responding fast enough " +
"(see Geocoder::Configuration.timeout to set limit)." "(see Geocoder::Configuration.timeout to set limit)."
end end
end end
......
...@@ -357,6 +357,19 @@ class GeocoderTest < Test::Unit::TestCase ...@@ -357,6 +357,19 @@ class GeocoderTest < Test::Unit::TestCase
end end
def test_always_raise_timeout_error
Geocoder::Configuration.always_raise = [TimeoutError]
assert_raise(TimeoutError) { Geocoder.search("timeout") }
Geocoder::Configuration.always_raise = []
end
def test_always_raise_socket_error
Geocoder::Configuration.always_raise = [SocketError]
assert_raise(SocketError) { Geocoder.search("socket_error") }
Geocoder::Configuration.always_raise = []
end
# --- Google --- # --- Google ---
def test_google_result_components def test_google_result_components
......
...@@ -62,6 +62,7 @@ module Geocoder ...@@ -62,6 +62,7 @@ module Geocoder
private #----------------------------------------------------------------- private #-----------------------------------------------------------------
def fetch_raw_data(query, reverse = false) def fetch_raw_data(query, reverse = false)
raise TimeoutError if query == "timeout" raise TimeoutError if query == "timeout"
raise SocketError if query == "socket_error"
file = case query file = case query
when "no results"; :no_results when "no results"; :no_results
when "no locality"; :no_locality when "no locality"; :no_locality
...@@ -76,6 +77,7 @@ module Geocoder ...@@ -76,6 +77,7 @@ module Geocoder
private #----------------------------------------------------------------- private #-----------------------------------------------------------------
def fetch_raw_data(query, reverse = false) def fetch_raw_data(query, reverse = false)
raise TimeoutError if query == "timeout" raise TimeoutError if query == "timeout"
raise SocketError if query == "socket_error"
file = case query file = case query
when "no results"; :no_results when "no results"; :no_results
else :madison_square_garden else :madison_square_garden
...@@ -88,6 +90,7 @@ module Geocoder ...@@ -88,6 +90,7 @@ module Geocoder
private #----------------------------------------------------------------- private #-----------------------------------------------------------------
def fetch_raw_data(query, reverse = false) def fetch_raw_data(query, reverse = false)
raise TimeoutError if query == "timeout" raise TimeoutError if query == "timeout"
raise SocketError if query == "socket_error"
file = case query file = case query
when "no results"; :no_results when "no results"; :no_results
when "invalid key"; :invalid_key when "invalid key"; :invalid_key
...@@ -101,6 +104,7 @@ module Geocoder ...@@ -101,6 +104,7 @@ module Geocoder
private #----------------------------------------------------------------- private #-----------------------------------------------------------------
def fetch_raw_data(query, reverse = false) def fetch_raw_data(query, reverse = false)
raise TimeoutError if query == "timeout" raise TimeoutError if query == "timeout"
raise SocketError if query == "socket_error"
if reverse if reverse
read_fixture "geocoder_ca_reverse.json" read_fixture "geocoder_ca_reverse.json"
else else
...@@ -117,6 +121,7 @@ module Geocoder ...@@ -117,6 +121,7 @@ module Geocoder
private #----------------------------------------------------------------- private #-----------------------------------------------------------------
def fetch_raw_data(query, reverse = false) def fetch_raw_data(query, reverse = false)
raise TimeoutError if query == "timeout" raise TimeoutError if query == "timeout"
raise SocketError if query == "socket_error"
read_fixture "freegeoip_74_200_247_59.json" read_fixture "freegeoip_74_200_247_59.json"
end end
end end
...@@ -125,6 +130,7 @@ module Geocoder ...@@ -125,6 +130,7 @@ module Geocoder
private #----------------------------------------------------------------- private #-----------------------------------------------------------------
def fetch_raw_data(query, reverse = false) def fetch_raw_data(query, reverse = false)
raise TimeoutError if query == "timeout" raise TimeoutError if query == "timeout"
raise SocketError if query == "socket_error"
if reverse if reverse
read_fixture "bing_reverse.json" read_fixture "bing_reverse.json"
else else
......
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