area: apiarea: productionbughelp wanted
Description
Right now, if the server 500s on a JSON request from a client in a way that doesn't return valid JSON, we get this sort of JS exception sent back to the server.
Message:
Unexpected token < in JSON at position 0
Stacktrace:
SyntaxError: Unexpected token < in JSON at position 0
at JSON.parse (<anonymous>)
at Object.a.xhr_error_message (static/min/app.f993cb5274ab.js:1331:372)
= static/js/channel.js line 110 column 27
at error (static/min/app.f993cb5274ab.js:2062:742)
= static/js/reactions.js line 25 column 28
I think we should fix this in 2 ways;
- Make it ~impossible for an invalid-JSON response to come back from
channel.js(e.g. have a set of 500 error pages for API URLs that are in JSON format), and mostly not change this code. This probably involves changes to both ournginxconfiguration (for "unexpected errors" where Django isn't even running). And then there's a probably simpler piece of changingzerver/middleware.pyto handle unexpected exceptions in JSON style API routes at the Django level. - Make
channel.jscatch this exception and provide a more clear error message for what's happening (perhaps including the invalid JSON it received) in the report, viablueslip.erroror something.