relay-tools / react-relay-network-layer

ReactRelayNetworkLayer with middlewares and query batching for Relay Classic.
MIT License
277 stars 47 forks source link

Handle malformed or non existent JSON in response #20

Closed JonathanUsername closed 8 years ago

JonathanUsername commented 8 years ago

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!).

st0ffern commented 8 years ago

@JonathanUsername he is on vacation and i think he will look into this when he is back 😉

nodkz commented 8 years ago

@JonathanUsername thanks for PR. Bumped version 1.3.5

PS. Sorry for delay, I was on vacation.