RESO-2736: Improve error handling with non-JSON response bodies #36
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.
Two errors were mentioned in the original ticket.
Cannot read property 'statusCode' of undefined
. This is caused on line 33 by trying to readstatusCode
from ares
which might be undefined (it will be in the case thaterr
is truthy), since that's an error thrown locally before a request is made, and so there will be no response. The fix here is applied on line 33.Unexpected token < in JSON at position 1 at JSON.parse
. This one is more apparent; the body in some cases us html and thus not JSON parsable. I applied a function to try to parse and to just replace withundefined
if the parse fails. I don't love this solution and I'm very open to others. I assume error only happens in the case of non-200 status responses, but I applied the change for the success branch (the last if-else chain item) also just in case.