Intended outcome:
Called an Apollo Service which returns HTTP 403 error. Since the body response contains the following HTML, the client should just ignore the content.
<!DOCTYPE html>\n<html lang="en">\n<head>\n<meta charset="utf-8">\n<title>Error</title>\n</head>\n<body>\n<pre>Error: Forbidden</pre>\n</body>\n</html>\n
Actual outcome:
Apollo Client tries to read the HTML interpreting it as JSON and fails with error: Unexpected token < in JSON at position 0.
How to reproduce the issue:
Call any Apollo service with Apollo Client which returns HTTP code != 200 with the content of the HTTP response being in HTML format.
Version
Question
I can work around this problem by wrapping all calls into a try { } catch() { } block, then reading out the status code, checking the type of the body response and handling the exception. However, I find this very cumbersome as I would have to do this with every call. Is there a way to globally configure Apollo Client to not try to parse the HTTP response body if it is anything else than JSON?
Another alternative would be to not parse HTTP response bodies if the status code is anything else than 2xx.
Duplicate of https://github.com/apollographql/apollo-client/issues/2770. Closing here - thanks!
Most helpful comment
Another alternative would be to not parse HTTP response bodies if the status code is anything else than 2xx.