Fix empty response from matrix

https://github.com/CartoDB/dataservices-api/issues/293
This commit is contained in:
Mario de Frutos 2016-10-26 10:23:41 +02:00
parent 0d22942a72
commit 19d6cacdb3
3 changed files with 18 additions and 6 deletions

View File

@ -105,14 +105,16 @@ class MapzenIsolines:
response = self._matrix_client.one_to_many([origin] + location_estimates, costing_model) response = self._matrix_client.one_to_many([origin] + location_estimates, costing_model)
costs = [None] * self.NUMBER_OF_ANGLES costs = [None] * self.NUMBER_OF_ANGLES
if not response:
# In case the matrix client doesn't return any data
break
for idx, c in enumerate(response['one_to_many'][0][1:]): for idx, c in enumerate(response['one_to_many'][0][1:]):
if c[cost_variable]: if c[cost_variable]:
costs[idx] = c[cost_variable]*unit_factor costs[idx] = c[cost_variable]*unit_factor
else: else:
costs[idx] = isorange costs[idx] = isorange
# self._logger.debug('i = %d, costs = %s' % (i, costs))
errors = [(cost - isorange) / float(isorange) for cost in costs] errors = [(cost - isorange) / float(isorange) for cost in costs]
max_abs_error = max([abs(e) for e in errors]) max_abs_error = max([abs(e) for e in errors])
if max_abs_error <= self.TOLERANCE: if max_abs_error <= self.TOLERANCE:

View File

@ -43,7 +43,7 @@ class MatrixClient:
} }
response = requests.get(self.ONE_TO_MANY_URL, params=request_params) response = requests.get(self.ONE_TO_MANY_URL, params=request_params)
if not requests.codes.ok: if response.status_code != requests.codes.ok:
self._logger.error('Error trying to get matrix distance from mapzen', self._logger.error('Error trying to get matrix distance from mapzen',
data={"response_status": response.status_code, data={"response_status": response.status_code,
"response_reason": response.reason, "response_reason": response.reason,
@ -52,6 +52,16 @@ class MatrixClient:
"response_headers": response.headers, "response_headers": response.headers,
"locations": locations, "locations": locations,
"costing": costing}) "costing": costing})
raise ServiceException("Error trying to get matrix distance from mapzen", response) # In case 4xx error we return empty because the error comes from
# the provided info by the user and we don't want to top the
# isolines generation
if response.status_code == requests.codes.bad_request:
return {}
else:
raise ServiceException("Error trying to get matrix distance from mapzen", response)
return response.json() # response could return with empty json
try:
return response.json()
except:
return {}

View File

@ -10,7 +10,7 @@ from setuptools import setup, find_packages
setup( setup(
name='cartodb_services', name='cartodb_services',
version='0.9.2', version='0.9.3',
description='CartoDB Services API Python Library', description='CartoDB Services API Python Library',