zulip/zulip

channel: Handle 500s from the server more naturally

Open

#6,296 opened on Aug 28, 2017

View on GitHub
 (12 comments) (0 reactions) (0 assignees)Python (19,672 stars) (7,339 forks)batch import
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 our nginx configuration (for "unexpected errors" where Django isn't even running). And then there's a probably simpler piece of changing zerver/middleware.py to handle unexpected exceptions in JSON style API routes at the Django level.
  • Make channel.js catch this exception and provide a more clear error message for what's happening (perhaps including the invalid JSON it received) in the report, via blueslip.error or something.

Contributor guide