From 3cc4e06420dbbb58739fae686f3b18383030ab82 Mon Sep 17 00:00:00 2001 From: Mario de Frutos Date: Wed, 17 Aug 2016 11:41:29 +0200 Subject: [PATCH] Add more logs for mapzen services --- .../cartodb_services/here/routing.py | 11 +++++-- .../cartodb_services/mapzen/geocoder.py | 32 +++++++++++++++---- .../cartodb_services/mapzen/matrix_client.py | 9 ++++-- .../cartodb_services/mapzen/qps.py | 3 +- .../cartodb_services/mapzen/routing.py | 14 +++++--- server/lib/python/cartodb_services/setup.py | 2 +- 6 files changed, 52 insertions(+), 19 deletions(-) diff --git a/server/lib/python/cartodb_services/cartodb_services/here/routing.py b/server/lib/python/cartodb_services/cartodb_services/here/routing.py index 5c01f84..a6f8486 100644 --- a/server/lib/python/cartodb_services/cartodb_services/here/routing.py +++ b/server/lib/python/cartodb_services/cartodb_services/here/routing.py @@ -57,9 +57,14 @@ class HereMapsRoutingIsoline: return [] else: self._logger.error('Error trying to calculate HERE isolines', - data={"response": response.json(), "source": - source, "mode": mode, "data_range": - data_range, "range_type": range_type, + data={"response_status": response.status_code, + "response_reason": response.reason, + "response_content": response.text, + "reponse_url": response.url, + "response_headers": response.headers, + "source": source, "mode": mode, + "data_range": data_range, + "range_type": range_type, "options": options}) raise Exception('Error trying to calculate HERE isolines') diff --git a/server/lib/python/cartodb_services/cartodb_services/mapzen/geocoder.py b/server/lib/python/cartodb_services/cartodb_services/mapzen/geocoder.py index 5816950..3049509 100644 --- a/server/lib/python/cartodb_services/cartodb_services/mapzen/geocoder.py +++ b/server/lib/python/cartodb_services/cartodb_services/mapzen/geocoder.py @@ -19,14 +19,32 @@ class MapzenGeocoder: @qps_retry def geocode(self, searchtext, city=None, state_province=None, country=None): - request_params = self._build_requests_parameters(searchtext, city, state_province, country) - response = requests.get(self._url, params=request_params) - if response.status_code == requests.codes.ok: - return self.__parse_response(response.text) - elif response.status_code == requests.codes.bad_request: + request_params = self._build_requests_parameters(searchtext, city, + state_province, + country) + try: + response = requests.get(self._url, params=request_params) + if response.status_code == requests.codes.ok: + return self.__parse_response(response.text) + elif response.status_code == requests.codes.bad_request: + return [] + else: + self._logger.error('Error trying to geocode using mapzen', + data={"response_status": response.status_code, + "response_reason": response.reason, + "response_content": response.text, + "reponse_url": response.url, + "response_headers": response.headers, + "searchtext": searchtext, + "city": city, "country": country, + "state_province": state_province }) + raise Exception('Error trying to geocode {0} using mapzen'.format(searchtext)) + except requests.ConnectionError as e: + # Don't raise the exception to continue with the geocoding job + self._logger.error('Error connecting to Mapzen geocoding server', + exception=e) return [] - else: - raise Exception('Error trying to geocode {0} using mapzen'.format(searchtext)) + def _build_requests_parameters(self, searchtext, city=None, state_province=None, country=None): diff --git a/server/lib/python/cartodb_services/cartodb_services/mapzen/matrix_client.py b/server/lib/python/cartodb_services/cartodb_services/mapzen/matrix_client.py index f5e00a7..375c7ea 100644 --- a/server/lib/python/cartodb_services/cartodb_services/mapzen/matrix_client.py +++ b/server/lib/python/cartodb_services/cartodb_services/mapzen/matrix_client.py @@ -44,8 +44,13 @@ class MatrixClient: if not requests.codes.ok: self._logger.error('Error trying to get matrix distance from mapzen', - data={"response": response, "locations": - locations, "costing": costing}) + data={"response_status": response.status_code, + "response_reason": response.reason, + "response_content": response.text, + "reponse_url": response.url, + "response_headers": response.headers, + "locations": locations, + "costing": costing}) raise Exception('Error trying to get matrix distance from mapzen') return response.json() diff --git a/server/lib/python/cartodb_services/cartodb_services/mapzen/qps.py b/server/lib/python/cartodb_services/cartodb_services/mapzen/qps.py index 21c45a4..f999804 100644 --- a/server/lib/python/cartodb_services/cartodb_services/mapzen/qps.py +++ b/server/lib/python/cartodb_services/cartodb_services/mapzen/qps.py @@ -26,7 +26,8 @@ class QPSService: try: return fn(*args, **kwargs) except Exception as e: - if hasattr(e, 'response') and (e.response.status_code == 429): + response = getattr(e, 'response', None) + if response and (response.status_code == 429): self.retry(start_time, attempt_number) else: raise e diff --git a/server/lib/python/cartodb_services/cartodb_services/mapzen/routing.py b/server/lib/python/cartodb_services/cartodb_services/mapzen/routing.py index 72a9841..dab21b5 100644 --- a/server/lib/python/cartodb_services/cartodb_services/mapzen/routing.py +++ b/server/lib/python/cartodb_services/cartodb_services/mapzen/routing.py @@ -49,11 +49,15 @@ class MapzenRouting: elif response.status_code == requests.codes.bad_request: return MapzenRoutingResponse(None, None, None) else: - self._logger.error('Error trying to calculate route using HERE', - data={"response": response.json(), "waypoints": - waypoints, "mode": mode, "options": - options}) - raise Exception('Error trying to calculate route using HERE') + self._logger.error('Error trying to calculate route using Mapzen', + data={"response_status": response.status_code, + "response_reason": response.reason, + "response_content": response.text, + "reponse_url": response.url, + "response_headers": response.headers, + "waypoints": waypoints, "mode": mode, + "options": options}) + raise Exception('Error trying to calculate route using Mapzen') def __parse_options(self, options): return dict(option.split('=') for option in options) diff --git a/server/lib/python/cartodb_services/setup.py b/server/lib/python/cartodb_services/setup.py index 42c7426..04e18a2 100644 --- a/server/lib/python/cartodb_services/setup.py +++ b/server/lib/python/cartodb_services/setup.py @@ -10,7 +10,7 @@ from setuptools import setup, find_packages setup( name='cartodb_services', - version='0.7.4.1', + version='0.7.4.2', description='CartoDB Services API Python Library',