-
Notifications
You must be signed in to change notification settings - Fork 193
NodeJS crash on reportState JSON.parse() #411
Comments
You are sending an HTML response into the |
I think JorgeLosadaM meant the response to the reportState (which is sent from Google) contained a non-JSON response (probably HTML). We are facing the same issue. From time to time, our node crashed because of this: Unexpected token < in JSON at position 0 SyntaxError: Unexpected token < in JSON at position 0 |
Yes, sometimes the response to the reportState contained HTML which lead to node crashes. We solved this issue temporarily with a fork of the repository until we could upgrade to node12, but for older versions the issue is still relevant. |
Hi JorgeLosadaM, Is it possible to share the fork with us? Does the node version matter? We're using node 10. |
We do not have de project forked anymore, but I strongly suggest you to upgrade node to at least node12 because it can handle the type of error this issue raises. I hope this helps. |
Also if you want to create a fork yourself instead of updating node, i think the fix was changing the line 301 in src/service/smarthome/smarthome.ts to this one: |
Thanks JorgeLosadaM |
The makeApiCall function creates an HTTP request to google's server, and in the POST headers, it does not specify the header "Accept: application/json". It seems that in (at least some) cases of error in Google's server, it returns back xml. Setting the accept header to "application/json" will likely fix this crash from occurring. The second thing is that if JSON.parse throws an error, there is not any try/catch to catch the error. |
Version:
2.10.0 (Despite the version we use is older, the bug is not patched in recent versions)
Node Version: 6.13.0
Description:
When a html response is feeded to the method
reportState
asapiResponse
in the event'end'
, the function JSON.parse(apiResponse) crashes node with this output:Some names are masked for client privacy reasons
Conclusion:
The error can not be catched with a try/catch structure, so before doing JSON.parse(), apiResponse has to be validated to make sure it is a stringified JSON object.
The text was updated successfully, but these errors were encountered: