diff --git a/mapit/shortcuts.py b/mapit/shortcuts.py index 62f9427b..4fa2340b 100644 --- a/mapit/shortcuts.py +++ b/mapit/shortcuts.py @@ -101,6 +101,10 @@ def json_500(request): return output_json({'error': "Sorry, something's gone wrong."}, code=500) +def json_404(request, *args, **kwargs): + return output_json({'error': "Not found."}, code=404) + + def set_timeout(format): cursor = connection.cursor() timeout = 10000 if format == 'html' else 10000 diff --git a/mapit/urls.py b/mapit/urls.py index 53e5cd4a..9e5546de 100644 --- a/mapit/urls.py +++ b/mapit/urls.py @@ -2,6 +2,7 @@ from django.conf import settings from django.shortcuts import render +from mapit.shortcuts import json_404 from mapit.utils import re_number as number from mapit.views import areas, postcodes @@ -58,6 +59,12 @@ re_path(r'^areas/(?P.+?)%s$' % map_format_end, areas.areas_by_name), re_path(r'^areas$', areas.deal_with_POST, {'call': 'areas'}), re_path(r'^code/(?P[^/]+)/(?P[^/]+?)%s$' % format_end, areas.area_from_code), + + # Catch API-shaped URLs that didn't match any of the specific patterns + # above and return a JSON 404 instead of Django's HTML one. Non-API URLs + # still fall through to the project-level 404 page. + re_path(r'^(?:postcode|area|areas|point|nearest|code)(?:/.*|\.[a-z]+)?$', json_404), + re_path(r'^(?:generations|types)(?:\.[a-z]+)?$', json_404), ] # Include app-specific urls