Open JorgeLosadaM opened 4 years ago
You are sending an HTML response into the reportState
method?
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
at JSON.parse (
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. The problem with older versions is that it directly crashes the node instance, in node12 we catch the error with a simple try...catch structure and we ignore it.
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:
const apiResponseJson = JSON.parse(JSON.stringify(apiResponse))
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.