From 082bac8c3a3d4709d558f0bd248239f104f229bc Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Mon, 22 Mar 2021 21:06:59 +0000 Subject: [PATCH] Handle invalid regex set dynamically in Switch node Fixes #2905 --- .../nodes/core/function/10-switch.js | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/packages/node_modules/@node-red/nodes/core/function/10-switch.js b/packages/node_modules/@node-red/nodes/core/function/10-switch.js index 2d91e4b8d..9d1793817 100644 --- a/packages/node_modules/@node-red/nodes/core/function/10-switch.js +++ b/packages/node_modules/@node-red/nodes/core/function/10-switch.js @@ -177,11 +177,17 @@ module.exports = function(RED) { getV1(node,msg,rule,state.hasParts, (err,value) => { if (err) { + // This only happens if v1 is an invalid JSONata expr + // But that will have already been logged and the node marked + // invalid as part of the constructor return done(err); } v1 = value; getV2(node,msg,rule, (err,value) => { if (err) { + // This only happens if v1 is an invalid JSONata expr + // But that will have already been logged and the node marked + // invalid as part of the constructor return done(err); } v2 = value; @@ -189,16 +195,22 @@ module.exports = function(RED) { property = state.elseflag; state.elseflag = true; } - if (operators[rule.t](property,v1,v2,rule.case,msg.parts)) { - state.onward.push(msg); - state.elseflag = false; - if (node.checkall == "false") { - return done(undefined,false); + try { + if (operators[rule.t](property,v1,v2,rule.case,msg.parts)) { + state.onward.push(msg); + state.elseflag = false; + if (node.checkall == "false") { + return done(undefined,false); + } + } else { + state.onward.push(null); } - } else { - state.onward.push(null); + done(undefined, state.currentRule < node.rules.length - 1); + } catch(err) { + // An error occurred evaluating the rule - for example, an + // invalid RegExp value. + done(err); } - done(undefined, state.currentRule < node.rules.length - 1); }); }); } @@ -437,7 +449,7 @@ module.exports = function(RED) { } else { applyRules(node,msg,property,undefined,(err,onward) => { if (err) { - node.warn(err); + node.error(err, msg); } else { if (!repair || !hasParts) { node.send(onward);