From 890763b384a2a0c7843c85714bac4fa19621cc8b Mon Sep 17 00:00:00 2001 From: Matt <927830+mattmattmatt@users.noreply.github.com> Date: Sun, 8 Apr 2018 00:50:20 -0700 Subject: [PATCH] fix: catch rejected promises from Yeelight lib in State node --- src/YeeLightNodeState.js | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/src/YeeLightNodeState.js b/src/YeeLightNodeState.js index 27a9fc1..8627f48 100644 --- a/src/YeeLightNodeState.js +++ b/src/YeeLightNodeState.js @@ -11,17 +11,32 @@ export default function YeeLightNodeState(RED) { // on, hex, bri, hue, sat, duration const onInput = msg => { - node.serverConfig.yeelight.sync().then(state => { - msg.payload = sanitizeState(state); - // only send message if new information or if requested by input - if ( - JSON.stringify(msg.payload) !== JSON.stringify(lastSentState) || - Object.keys(msg).length > 1 - ) { - node.send(msg); - lastSentState = msg.payload; - } - }); + node.serverConfig.yeelight + .sync() + .then(state => { + msg.payload = sanitizeState(state); + // only send message if new information or if requested by input + if ( + JSON.stringify(msg.payload) !== JSON.stringify(lastSentState) || + Object.keys(msg).length > 1 + ) { + node.send(msg); + lastSentState = msg.payload; + } + }) + .catch(e => { + if (e.message === 'timeout') { + e.code = 'timeout'; + e.message = 'Local timeout in "Yeelight.command" execution'; + } + node.log('An error occured while syncing or setting a new value'); + console.error(e); + node.status({ + fill: 'red', + shape: 'ring', + text: `Command send error: ${e.code}`, + }); + }); }; (function init() {