From 66a667fe585722875c267b76e99cf981d94e47ce Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Thu, 9 May 2024 16:48:51 +0100 Subject: [PATCH] Pass full error object in Function node and copy over cause property Fixes #4683 --- .../@node-red/nodes/core/function/10-function.js | 15 ++++++++------- .../@node-red/runtime/lib/flows/Flow.js | 3 +++ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/packages/node_modules/@node-red/nodes/core/function/10-function.js b/packages/node_modules/@node-red/nodes/core/function/10-function.js index a915a1623..580b115f0 100644 --- a/packages/node_modules/@node-red/nodes/core/function/10-function.js +++ b/packages/node_modules/@node-red/nodes/core/function/10-function.js @@ -438,10 +438,9 @@ module.exports = function(RED) { //store the error in msg to be used in flows msg.error = err; - - var line = 0; - var errorMessage; if (stack.length > 0) { + let line = 0; + let errorMessage; while (line < stack.length && stack[line].indexOf("ReferenceError") !== 0) { line++; } @@ -455,11 +454,13 @@ module.exports = function(RED) { errorMessage += " (line "+lineno+", col "+cha+")"; } } + if (errorMessage) { + err.message = errorMessage + } } - if (!errorMessage) { - errorMessage = err.toString(); - } - done(errorMessage); + // Pass the whole error object so any additional properties + // (such as cause) are preserved + done(err); } else if (typeof err === "string") { done(err); diff --git a/packages/node_modules/@node-red/runtime/lib/flows/Flow.js b/packages/node_modules/@node-red/runtime/lib/flows/Flow.js index dd3d335a2..c4f4e39a2 100644 --- a/packages/node_modules/@node-red/runtime/lib/flows/Flow.js +++ b/packages/node_modules/@node-red/runtime/lib/flows/Flow.js @@ -678,6 +678,9 @@ class Flow { if (logMessage.hasOwnProperty('stack')) { errorMessage.error.stack = logMessage.stack; } + if (logMessage.hasOwnProperty('cause')) { + errorMessage.error.cause = logMessage.cause; + } targetCatchNode.receive(errorMessage); handled = true; });