Homey Community Forum

JSON.parse promise error

#1

Not sure what I do wrong here, the code I execute:

this.ws.on(‘message’, (json) => {
let messageData = {};
if(json) {
try {
messageData = JSON.parse(json);
} catch (err) {
messageData = {};
console.log(err);
}
}

but it returns in the following error:

(node:26596) UnhandledPromiseRejectionWarning: Error: invalid_type
at Object.JSON.parse (/opt/homey-client/system/helpers/jsonfns.js:1:1190)
at JSON.parse (<anonymous>)
at jsonToObject (/opt/homey-client/system/helpers/jsonfns.js:1:1083)
at function.e.data.map.e (/opt/homey-client/system/manager/ManagerApps/bootstrap/sdk/v2/lib/HomeyClient.js:1:1379)
at Array.map (<anonymous>)
at HomeyClient._onMessage (/opt/homey-client/system/manager/ManagerApps/bootstrap/sdk/v2/lib/HomeyClient.js:1:1372)
at emitTwo (events.js:126:13)
at process.emit (events.js:214:7)
at emit (internal/child_process.js:762:12)
at _combinedTickCallback (internal/process/next_tick.js:142:11)
(node:26596) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:26596) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I was not aware that JSON.parse was async, nor that excuting it within a try catch could cause something like this. I want to prevent my driver from exiting so how would I prevent this error?
If for some reason I get invalid responses from my client I need to be able to handle those.

#2

The error is thrown somewhere inside Homey’s homey-client lib, not in your code.

#3

So this is nothing I need to worry about?

#4

It’s still an error that you should track down (not sure what invalid_type means, perhaps it expects a Number but you pass a String somewhere), but it doesn’t look like it’s caused by the code you posted.