New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to handle error from JSON.parse #61
Comments
Personally, I think Promise rejection is a good solution. At the very least, one could invoke |
I also think there should be an easy option to retry the request in these cases as it often boils down to a truncated response. |
Just got burned on this. Instead of correctly rejecting the promise on invalid (truncated) JSON, it simply returns the orignal string object. Bad bad bad. At least put a warning in the docs about this. |
I think we should just reject with the parse errors. That'd make axios more predictable and easier to debug. |
I also spent 1 hour today tryng to understand what is happening with some invalid JSON. Just reject the Promise, or at least console.log something...? |
imagine first finding out in production - not sure where the logical thinking has gone on this one |
Not sure how others are avoiding this issue after almost 3 years, but I decided to try to work around it for one of my projects. I would like to see how other people have been doing this as well. I took notes from here and only raise an error for 2xx response codes. If people find it useful I can make a PR of my commit. If you need this to raise for other response codes, just remove lines 67 and 71 in Another option for those who don’t want to fork is to just override transformResponse, and detect when data is a string, and parse on your own. Unfortunately this means you’ll do more work, and you can’t check the status code if you do that, but at least you have options. |
I also encountered a scenario where this behavior is problematic. If the network connection is interrupted partially through the request, axios would not call an error and my application received malformed JSON which obviously led to lots of errors. I too think that JSON parse errors should be caught and reported. I wasn't keen on forking or modifying the source files so my solution was to transform the response like this:
|
My approach to handling this is to wrap all axios requests in a promise, and reject that promise if the response from axios show a response.headers['content-type'] of 'application/json' and a response.data that is a string. From this I infer there was a JSON parsing error.
|
@emilyemorehouse is this something you could fix, for the sake of all humanity? |
Should throw exception |
+1 here, swallowing errors is never good |
This is among the most disappointing responses to a glaring issue I've seen in an open-source project. This has been an issue for almost 4 years... This is the reason I use my own HTTP package. |
@vinnymac Can You please submit PR of Your solution? |
Just came across this as well, it seems if I manually add: |
I just got bitten by this in production today. The current behavior is really unintuitive and wasted a bunch of my time in debugging. Throwing an error would have pointed me in the right direction immediately and would have been helpfully caught/grouped in our error reporting. Even once I found that there was a disconnect between the type of data I got in the response vs. what the header said it should be, it wasn't trivial to find this issue as the explanation. |
Is this responseText a problem? like "24\r\n{"success":"true", "payload":"123456"}\r\n0\r\n\r\n", and sometimes it returns "24\r\n{"success":"true", "payload":"123456"}\r\n". Is there anyone who can help, thanks a lot. |
Generally speaking, that was exactly what we faced.... |
Guys, any updates on this issue? |
…be passed directly as payload for encoding to JSON #2613, #61, #907 (#3688) * Draft * Added support for primitive types to be converted to JSON if the request Content-Type is 'application/json'; Added throwing SyntaxError if JSON parsing failed and responseType is json; Added transitional option object; Added options validator to assert transitional options; Added transitional option `silentJSONParsing= true` for backward compatibility; Updated README.md; Updated typings; * Fixed isOlderVersion helper; Fixed typo; Added validator.spec.js; * Added forcedJSONParsing transitional option #2791 * `transformData` is now called in the default configuration context if the function context is not specified (for tests compatibility); * Added `transitional.clarifyTimeoutError` to throw ETIMEDOUT error instead of generic ECONNABORTED on request timeouts; Added support of onloadend handler if available instead of onreadystatechange; Added xhr timeout test; Fixed potential bug of xhr adapter with proper handling timeouts&errors (FakeXMLHTTPRequest failed to handle timeouts);
Fixed in #3688 |
I see this has been closed already, but imo neither of the available behaviours from silentJSONParsing are quite perfect for all use cases. Effectively it seems like I have to choose between being able to know the status code or the JSON parsing error, but not both. |
…be passed directly as payload for encoding to JSON axios#2613, axios#61, axios#907 (axios#3688) * Draft * Added support for primitive types to be converted to JSON if the request Content-Type is 'application/json'; Added throwing SyntaxError if JSON parsing failed and responseType is json; Added transitional option object; Added options validator to assert transitional options; Added transitional option `silentJSONParsing= true` for backward compatibility; Updated README.md; Updated typings; * Fixed isOlderVersion helper; Fixed typo; Added validator.spec.js; * Added forcedJSONParsing transitional option axios#2791 * `transformData` is now called in the default configuration context if the function context is not specified (for tests compatibility); * Added `transitional.clarifyTimeoutError` to throw ETIMEDOUT error instead of generic ECONNABORTED on request timeouts; Added support of onloadend handler if available instead of onreadystatechange; Added xhr timeout test; Fixed potential bug of xhr adapter with proper handling timeouts&errors (FakeXMLHTTPRequest failed to handle timeouts);
The
transformRequest
method attempts to parse JSON, but swallows any error in an emptycatch
statement. This results in the stringified form of the response body to be returned should the JSON be malformed, and supplied to the response object that is used to resolve thePromise
.Without the try/catch a global error would occur. With the try/catch the code handling the response will error, but in an unpredictable way. Consider:
In this case
user
is aString
not anObject
as expected. Calling'some string'.isBlocked
will returnundefined
, which evaluates as falsey. This will cause theif
condition to erroneously be entered.A potential solution would be to reject the
Promise
ifJSON.parse
throws anError
. This isn't a straight forward fix though as it would break #55 again.The text was updated successfully, but these errors were encountered: