From 54b63778b21077c352867814a2bc107cde59575d Mon Sep 17 00:00:00 2001 From: Matthew Somerville Date: Fri, 9 Jan 2015 11:04:04 +0000 Subject: [PATCH] Catch invalid SRID exceptions, return 400. Fixes #157. --- mapit/tests/test_views.py | 7 +++++++ mapit/views/postcodes.py | 2 ++ 2 files changed, 9 insertions(+) diff --git a/mapit/tests/test_views.py b/mapit/tests/test_views.py index 535145ab..91931a63 100644 --- a/mapit/tests/test_views.py +++ b/mapit/tests/test_views.py @@ -94,3 +94,10 @@ def test_example_postcode(self): response = self.client.get(url) content = json.loads(response.content.decode('utf-8')) self.assertEqual(content, self.postcode.postcode) + + def test_nearest_with_bad_srid(self): + url = '/nearest/84/0,0.json' + response = self.client.get(url) + self.assertEqual(response.status_code, 400) + content = json.loads(response.content.decode('utf-8')) + self.assertEqual(content, {'code': 400, 'error': 'GetProj4StringSPI: Cannot find SRID (84) in spatial_ref_sys\n'}) diff --git a/mapit/views/postcodes.py b/mapit/views/postcodes.py index 2f81141f..e68c010b 100644 --- a/mapit/views/postcodes.py +++ b/mapit/views/postcodes.py @@ -155,6 +155,8 @@ def nearest(request, srid, x, y, format='json'): postcode = Postcode.objects.filter( location__distance_gte=(location, D(mi=0))).distance(location).order_by('distance')[0] except DatabaseError as e: + if 'Cannot find SRID' in e.args[0]: + raise ViewException(format, e.args[0], 400) if 'canceling statement due to statement timeout' not in e.args[0]: raise raise ViewException(format, 'That query was taking too long to compute.', 500)