-
Notifications
You must be signed in to change notification settings - Fork 27.4k
JSON parse failure in defaultHttpResponseTransform #15695
Comments
I assume that by "debug [...] in production" you mean logging client errors to the server and analysing them to find issues. For that usecase, this makes sense. Of cource, you can create your own transform that captures that, but if someone wants to create a PR, I we can consider making this change in core. |
That's interesting, because prior to 1.6.0, a parse error wouldn't even be forwarded to the rejection handler / application code. Are people now finding new bugs in their apps because we let $q reject errors? |
I think it would (but in a slightly different way). Before 1.6:
After 1.6:
|
I assume that by "debug [...] in production" you mean logging client errors to the server and analysing them to find issues. For that usecase, this makes sense. Yes @gkalpak |
Hi, I'm interested in attempting a PR for this. Is there any suggested approach you want me to follow to fix this? |
Can we catch the exception in defaultHttpResponseTransform and reject with original data in cases where JSON data cannot be parsed? |
Off the top of my head, a reasonable approach would be to wrap the relevant code in a This should be a |
@chirag64: Thx for stepping up btw 👍 |
@chirag64 Yes, we definitely need a test for this. The tests are in httpSpec.js, this should be in the describe block for json parsing. Once you have cloned the repo you can run grunt autotest to start the karma server, which will then run all the tests. You can isolate a single test by changing the |
@Narretz So inside the |
There's actually a section about the json already in the httpSpec.js: angular.js/test/ng/httpSpec.js Line 1186 in 71f437c
There's a test about JSON deserialize errors: angular.js/test/ng/httpSpec.js Line 1372 in 71f437c
Instead of a SyntaxError, we have to match for a minErr now: angular.js/test/helpers/matchers.js Line 308 in 71f437c
It's also better to open a PR, so we can see the difference right away. @anupbelambe I don't really understand what you mean. |
A problem I see is that with this behavior, you won't have access to the raw response string (requested e.g. here #12012). Basically, there's two different requirements: a) specific error when parsing fails for debug reasons b) access to the original string for error recovery Can we achieve this together with a minErr? Wdyt @gkalpak? |
Sounds reasonable. We could embed the response (as string) into the minErr (and hope it is not huge 😛). |
Still unsure how to call |
v1.6.4 |
In defaultHttpResponseTransform() above, the response data is parsed as json, if either content type is application/json or is JSON like. In this case the data is unable to parsed as JSON in fromJson(), the exception is thrown at JSON.parse, which causes request to be rejected with empty object.
This makes it hard to debug response issues in production especially if response is dynamically generated, as no response data can be captured in this case.
My suggestion would be to reject with original response data in such cases and let client handle the failure.
The text was updated successfully, but these errors were encountered: