Skip to content

Commit

Permalink
Catch invalid SRID exceptions, return 400.
Browse files Browse the repository at this point in the history
Fixes #157.
  • Loading branch information
dracos committed Jan 9, 2015
1 parent d63fb95 commit 54b6377
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
7 changes: 7 additions & 0 deletions mapit/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'})
2 changes: 2 additions & 0 deletions mapit/views/postcodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 54b6377

Please sign in to comment.