TomTom error handling
This commit is contained in:
parent
bcb34d1cea
commit
07f5be9207
@ -1,6 +1,7 @@
|
|||||||
import json, requests, time
|
import json, requests, time
|
||||||
from requests.adapters import HTTPAdapter
|
from requests.adapters import HTTPAdapter
|
||||||
from cartodb_services import StreetPointBulkGeocoder
|
from cartodb_services import StreetPointBulkGeocoder
|
||||||
|
from cartodb_services.geocoder import geocoder_error_response
|
||||||
from cartodb_services.tomtom import TomTomGeocoder
|
from cartodb_services.tomtom import TomTomGeocoder
|
||||||
from cartodb_services.tools.exceptions import ServiceException
|
from cartodb_services.tools.exceptions import ServiceException
|
||||||
|
|
||||||
@ -43,13 +44,21 @@ class TomTomBulkGeocoder(TomTomGeocoder, StreetPointBulkGeocoder):
|
|||||||
return results
|
return results
|
||||||
|
|
||||||
def _batch_geocode(self, searches):
|
def _batch_geocode(self, searches):
|
||||||
location = self._send_batch(searches)
|
full_results = self._geocode_searches(searches)
|
||||||
full_results = self._download_results(location)
|
|
||||||
results = []
|
results = []
|
||||||
for s, r in zip(searches, full_results):
|
for s, r in zip(searches, full_results):
|
||||||
results.append((s[0], r[0], r[1]))
|
results.append((s[0], r[0], r[1]))
|
||||||
return results
|
return results
|
||||||
|
|
||||||
|
def _geocode_searches(self, searches):
|
||||||
|
try:
|
||||||
|
location = self._send_batch(searches)
|
||||||
|
return self._download_results(location)
|
||||||
|
except Exception as e:
|
||||||
|
msg = "Error running TomTom batch geocode: {}".format(e)
|
||||||
|
self._logger.error(msg, e)
|
||||||
|
return [geocoder_error_response(msg)] * len(searches)
|
||||||
|
|
||||||
def _send_batch(self, searches):
|
def _send_batch(self, searches):
|
||||||
body = {'batchItems': [{'query': self._query(s)} for s in searches]}
|
body = {'batchItems': [{'query': self._query(s)} for s in searches]}
|
||||||
request_params = {
|
request_params = {
|
||||||
|
@ -5,7 +5,7 @@ import json
|
|||||||
import requests
|
import requests
|
||||||
from uritemplate import URITemplate
|
from uritemplate import URITemplate
|
||||||
from math import tanh
|
from math import tanh
|
||||||
from cartodb_services.geocoder import PRECISION_PRECISE, PRECISION_INTERPOLATED, geocoder_metadata, EMPTY_RESPONSE
|
from cartodb_services.geocoder import PRECISION_PRECISE, PRECISION_INTERPOLATED, geocoder_metadata, EMPTY_RESPONSE, geocoder_error_response
|
||||||
from cartodb_services.metrics import Traceable
|
from cartodb_services.metrics import Traceable
|
||||||
from cartodb_services.tools.exceptions import ServiceException
|
from cartodb_services.tools.exceptions import ServiceException
|
||||||
from cartodb_services.tools.qps import qps_retry
|
from cartodb_services.tools.qps import qps_retry
|
||||||
@ -73,7 +73,12 @@ class TomTomGeocoder(Traceable):
|
|||||||
@qps_retry(qps=5)
|
@qps_retry(qps=5)
|
||||||
def geocode(self, searchtext, city=None, state_province=None,
|
def geocode(self, searchtext, city=None, state_province=None,
|
||||||
country=None):
|
country=None):
|
||||||
return self.geocode_meta(searchtext, city, state_province, country)[0]
|
response = self.geocode_meta(searchtext, city, state_province, country)
|
||||||
|
error_message = response[1].get('error', None)
|
||||||
|
if error_message:
|
||||||
|
raise ServiceException(error_message, None)
|
||||||
|
else:
|
||||||
|
return response[0]
|
||||||
|
|
||||||
@qps_retry(qps=5)
|
@qps_retry(qps=5)
|
||||||
def geocode_meta(self, searchtext, city=None, state_province=None,
|
def geocode_meta(self, searchtext, city=None, state_province=None,
|
||||||
@ -106,10 +111,9 @@ class TomTomGeocoder(Traceable):
|
|||||||
except requests.Timeout as te:
|
except requests.Timeout as te:
|
||||||
# In case of timeout we want to stop the job because the server
|
# In case of timeout we want to stop the job because the server
|
||||||
# could be down
|
# could be down
|
||||||
self._logger.error('Timeout connecting to TomTom geocoding server',
|
msg = 'Timeout connecting to TomTom geocoding server'
|
||||||
te)
|
self._logger.error(msg, te)
|
||||||
raise ServiceException('Error geocoding {0} using TomTom'.format(
|
return geocoder_error_response(msg)
|
||||||
searchtext), None)
|
|
||||||
except requests.ConnectionError as ce:
|
except requests.ConnectionError as ce:
|
||||||
# Don't raise the exception to continue with the geocoding job
|
# Don't raise the exception to continue with the geocoding job
|
||||||
self._logger.error('Error connecting to TomTom geocoding server',
|
self._logger.error('Error connecting to TomTom geocoding server',
|
||||||
@ -125,7 +129,9 @@ class TomTomGeocoder(Traceable):
|
|||||||
return EMPTY_RESPONSE
|
return EMPTY_RESPONSE
|
||||||
else:
|
else:
|
||||||
msg = 'Unknown response {}: {}'.format(str(status_code), text)
|
msg = 'Unknown response {}: {}'.format(str(status_code), text)
|
||||||
raise ServiceException(msg, None)
|
self._logger.warning('Error parsing TomTom geocoding response',
|
||||||
|
data={'msg': msg})
|
||||||
|
return geocoder_error_response(msg)
|
||||||
|
|
||||||
def _parse_geocoder_response(self, response):
|
def _parse_geocoder_response(self, response):
|
||||||
json_response = json.loads(response) \
|
json_response = json.loads(response) \
|
||||||
|
Loading…
Reference in New Issue
Block a user