From 07f5be92076070d1ace035d4984b61afba375097 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juan=20Ignacio=20S=C3=A1nchez=20Lara?= Date: Mon, 23 Jul 2018 17:17:38 +0200 Subject: [PATCH] TomTom error handling --- .../cartodb_services/tomtom/bulk_geocoder.py | 13 ++++++++++-- .../cartodb_services/tomtom/geocoder.py | 20 ++++++++++++------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/server/lib/python/cartodb_services/cartodb_services/tomtom/bulk_geocoder.py b/server/lib/python/cartodb_services/cartodb_services/tomtom/bulk_geocoder.py index 8fc2e92..400cc2a 100644 --- a/server/lib/python/cartodb_services/cartodb_services/tomtom/bulk_geocoder.py +++ b/server/lib/python/cartodb_services/cartodb_services/tomtom/bulk_geocoder.py @@ -1,6 +1,7 @@ import json, requests, time from requests.adapters import HTTPAdapter from cartodb_services import StreetPointBulkGeocoder +from cartodb_services.geocoder import geocoder_error_response from cartodb_services.tomtom import TomTomGeocoder from cartodb_services.tools.exceptions import ServiceException @@ -43,13 +44,21 @@ class TomTomBulkGeocoder(TomTomGeocoder, StreetPointBulkGeocoder): return results def _batch_geocode(self, searches): - location = self._send_batch(searches) - full_results = self._download_results(location) + full_results = self._geocode_searches(searches) results = [] for s, r in zip(searches, full_results): results.append((s[0], r[0], r[1])) 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): body = {'batchItems': [{'query': self._query(s)} for s in searches]} request_params = { diff --git a/server/lib/python/cartodb_services/cartodb_services/tomtom/geocoder.py b/server/lib/python/cartodb_services/cartodb_services/tomtom/geocoder.py index 3f077bd..b5ec592 100644 --- a/server/lib/python/cartodb_services/cartodb_services/tomtom/geocoder.py +++ b/server/lib/python/cartodb_services/cartodb_services/tomtom/geocoder.py @@ -5,7 +5,7 @@ import json import requests from uritemplate import URITemplate 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.tools.exceptions import ServiceException from cartodb_services.tools.qps import qps_retry @@ -73,7 +73,12 @@ class TomTomGeocoder(Traceable): @qps_retry(qps=5) def geocode(self, searchtext, city=None, state_province=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) def geocode_meta(self, searchtext, city=None, state_province=None, @@ -106,10 +111,9 @@ class TomTomGeocoder(Traceable): except requests.Timeout as te: # In case of timeout we want to stop the job because the server # could be down - self._logger.error('Timeout connecting to TomTom geocoding server', - te) - raise ServiceException('Error geocoding {0} using TomTom'.format( - searchtext), None) + msg = 'Timeout connecting to TomTom geocoding server' + self._logger.error(msg, te) + return geocoder_error_response(msg) except requests.ConnectionError as ce: # Don't raise the exception to continue with the geocoding job self._logger.error('Error connecting to TomTom geocoding server', @@ -125,7 +129,9 @@ class TomTomGeocoder(Traceable): return EMPTY_RESPONSE else: 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): json_response = json.loads(response) \