From 1d36ce0fdf16f2de4d46ae67be1cd7d27a1d0e2c Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Fri, 15 May 2020 23:32:27 +0100 Subject: [PATCH] debug node: add auto handling of error and status msgs if set to show status --- .../@node-red/nodes/core/common/21-debug.html | 17 +++--- .../@node-red/nodes/core/common/21-debug.js | 52 +++++++++++++------ .../nodes/locales/en-US/messages.json | 1 + 3 files changed, 48 insertions(+), 22 deletions(-) diff --git a/packages/node_modules/@node-red/nodes/core/common/21-debug.html b/packages/node_modules/@node-red/nodes/core/common/21-debug.html index c4640c66a..36ff0dc11 100644 --- a/packages/node_modules/@node-red/nodes/core/common/21-debug.html +++ b/packages/node_modules/@node-red/nodes/core/common/21-debug.html @@ -52,7 +52,7 @@ complete: {value:"false", required:true}, targetType: {value:undefined}, statusVal: {value:""}, - statusType: {value:"msg"} + statusType: {value:"auto"} }, label: function() { var suffix = ""; @@ -318,9 +318,14 @@ delete RED._debug; }, oneditprepare: function() { + var autoType = { + value: "auto", + label: RED._("node-red:debug.autostatus"), + hasValue: false + }; $("#node-input-typed-status").typedInput({ - default: "msg", - types:['msg',"jsonata"], + default: "auto", + types:[autoType, "msg", "jsonata"], typeField: $("#node-input-statusType") }); var that = this; @@ -339,7 +344,7 @@ } if (this.statusType === undefined) { this.statusType = this.targetType; - $("#node-input-typed-status").typedInput('type',this.statusType || "msg"); + $("#node-input-typed-status").typedInput('type',this.statusType || "auto"); } if (typeof this.console === "string") { this.console = (this.console == 'true'); @@ -385,7 +390,7 @@ if (type !== 'full') { comp = $("#node-input-typed-complete").typedInput('value'); } - that.statusType = (type !== "jsonata") ? "msg" : "jsonata"; + that.statusType = "auto"; that.statusVal = comp; } $("#node-input-typed-status").typedInput('type',that.statusType); @@ -394,7 +399,7 @@ } else { $("#node-tostatus-line").hide(); - that.statusType = "msg"; + that.statusType = "auto"; that.statusVal = ""; $("#node-input-typed-status").typedInput('type',that.statusType); $("#node-input-typed-status").typedInput('value',that.statusVal); diff --git a/packages/node_modules/@node-red/nodes/core/common/21-debug.js b/packages/node_modules/@node-red/nodes/core/common/21-debug.js index 8de1a1307..397b3a30c 100644 --- a/packages/node_modules/@node-red/nodes/core/common/21-debug.js +++ b/packages/node_modules/@node-red/nodes/core/common/21-debug.js @@ -69,18 +69,32 @@ module.exports = function(RED) { } function prepareStatus(msg, done) { - // Either apply the jsonata expression or... - if (preparedStatExpression) { - RED.util.evaluateJSONataExpression(preparedStatExpression, msg, (err, value) => { - if (err) { done(RED._("debug.invalid-exp", {error:editExpression})); } - else { done(null,{msg:value}); } - }); - } else { - // Extract the required message property - var output; - try { output = RED.util.getMessageProperty(msg,node.statusVal); } - catch(err) { output = undefined; } - done(null,{msg:output}); + if (node.statusType === "auto") { + if (node.complete === "true") { + done(null,{msg:msg.payload}); + } + else { + prepareValue(msg,function(err,debugMsg) { + if (err) { node.error(err); return; } + done(null,{msg:debugMsg.msg}); + }); + } + } + else { + // Either apply the jsonata expression or... + if (preparedStatExpression) { + RED.util.evaluateJSONataExpression(preparedStatExpression, msg, (err, value) => { + if (err) { done(RED._("debug.invalid-exp", {error:editExpression})); } + else { done(null,{msg:value}); } + }); + } + else { + // Extract the required message property + var output; + try { output = RED.util.getMessageProperty(msg,node.statusVal); } + catch(err) { output = undefined; } + done(null,{msg:output}); + } } } @@ -93,10 +107,16 @@ module.exports = function(RED) { if (st.length > 32) { st = st.substr(0,32) + "..."; } var fill = "grey"; var shape = "dot"; - if (node.statusVal === "error.message" && msg.hasOwnProperty("error")) { fill = "red"; } - if (node.statusVal === "status.text" && msg.hasOwnProperty("status")) { - if (msg.status.hasOwnProperty("fill")) { fill = msg.status.fill; } - if (msg.status.hasOwnProperty("shape")) { shape = msg.status.shape; } + if (node.statusType === "auto") { + if (msg.hasOwnProperty("error")) { + fill = "red"; + st = msg.error.message; + } + if (msg.hasOwnProperty("status")) { + if (msg.status.hasOwnProperty("fill")) { fill = msg.status.fill; } + if (msg.status.hasOwnProperty("shape")) { shape = msg.status.shape; } + if (msg.status.hasOwnProperty("text")) { st = msg.status.text; } + } } node.status({fill:fill, shape:shape, text:st}); }); diff --git a/packages/node_modules/@node-red/nodes/locales/en-US/messages.json b/packages/node_modules/@node-red/nodes/locales/en-US/messages.json index 76d1fcc3d..c2f85fcad 100755 --- a/packages/node_modules/@node-red/nodes/locales/en-US/messages.json +++ b/packages/node_modules/@node-red/nodes/locales/en-US/messages.json @@ -123,6 +123,7 @@ "invalid-exp": "Invalid JSONata expression: __error__", "msgprop": "message property", "msgobj": "complete msg object", + "autostatus": "automatic", "to": "To", "debtab": "debug tab", "tabcon": "debug tab and console",