Handle malformed or non existent JSON in response #20
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi, we have a number of reasons why the
/graphql
endpoint when hit will actually return HTML/XML or something that isn't valid JSON. For instance, if the user is not authenticated then we return a 403 page, this happens at the Nginx level.At the moment that means that we get an error coming through the middleware as a SyntaxError. This can't be gracefully dealt with by middleware other than by trying to catch that - if (error.name === 'SyntaxError') - and aside from that being ugly it also means the status code is ignored.
In this PR I'm trying to address this by catching any parsing errors arising from res.json() and returning a plain object instead. This is so that the status code can be handled correctly by thrownOnServerError where it throws the whole response, so that we can deal with the response.status inside our app (and direct the user to log in again!).