Return JSON for 404s, matching the rest of the API#444
Conversation
handler500 already routes 500s through output_json so API clients get a real error dict back. handler404 was the Django default, so any request that didn't match a URL pattern got the framework's HTML page back instead, even though the docs say MapIt always returns JSON. Mirror the pattern: add json_404 alongside json_500 and register it as handler404 in project/urls.py. Closes mysociety#421. Signed-off-by: Charlie Tonneslan <cst0520@gmail.com>
|
Thanks for your contribution, but I'm afraid this is too broad - we would still want people coming to the website and going to a page that doesn't exist (for whatever reason) to get a normal HTML 404 page, not a JSON response, which would only be for the API endpoints. Perhaps the solution is to allow more things through the url handler, and then have them dealt with there. |
Drop the global handler404 and instead match catch-all patterns for the API URL prefixes (postcode, area, areas, point, nearest, code, generations, types) at the end of mapit/urls.py. Non-API URLs keep falling through to the project-level HTML 404 page. Signed-off-by: Charlie Tonneslan <cst0520@gmail.com>
|
Good point, thanks. I dropped the global Spot-checked with the regexes locally:
Let me know if you'd rather inline the catch-all view instead of keeping |
|
I like the idea of fixing this one, thanks, hope to be able to take a look at it soon. |
Per #421 and @dracos' comment there, hitting a URL the router doesn't recognise currently falls through to Django's HTML 404 page even though the docs say the API always returns JSON.
handler500is already wired up tomapit.shortcuts.json_500; just mirror the pattern with ajson_404and register it ashandler404.get_object_or_404still goes throughViewExceptionso endpoint-level 404s are unchanged.Closes #421.