GraphQL client tries to parse 5xx HTML responses as JSON
Created by: felixfbecker
Sometimes for various reasons the backend (or proxy) responds with a 5xx HTTP error like 504: Gateway Timeout and a body that is Content-Type: text/html
.
Expected behavior:
When this happens, the GraphQL client in the webapp should see that the response is not a 200 OK response, and throw an error that surfaces the status and status text ("504 Gateway Timeout"). This at least gives some indication what happened, and that e.g. a reload could fix it. It should only proceed parsing a response as JSON if it has the status 200 OK.
I'm fairly certain this used to be the case before our switch to Apollo (i.e. the old requestGraphQL()
function checked the status code before parsing).
Actual behavior:
The GraphQL client still tries to parse the response as JSON, resulting in the error "Unexpected token in JSON at position 0" (from the HTML's <html>
tag). This is confusing for users and CEs trying to debug and looks like there is some kind of bug related to parsing, rather than something like a proxy timeout.
Example of this in the wild (we have seen this many times from customers):
/cc @taylorsperry @jasongornall