From f4d7b719843a0ae215914bf6532e875062c1df10 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Tue, 4 Oct 2022 15:20:06 +0100 Subject: [PATCH] Ensure errors in preDeliver callback are handled Fixes #3848 --- .../@node-red/runtime/lib/flows/Flow.js | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) 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 b5685d3ec..6da07f495 100644 --- a/packages/node_modules/@node-red/runtime/lib/flows/Flow.js +++ b/packages/node_modules/@node-red/runtime/lib/flows/Flow.js @@ -818,6 +818,17 @@ function handlePreRoute(flow, sendEvent, reportError) { }) } +function deliverMessageToDestination(sendEvent) { + if (sendEvent.destination.node) { + try { + sendEvent.destination.node.receive(sendEvent.msg); + } catch(err) { + const info = `${sendEvent.destination.node?.id}` + Log.error(`Error delivering message to node:${sendEvent.destination.node._path} [${sendEvent.destination.node.type}]`) + Log.error(err.stack) + } + } +} function handlePreDeliver(flow,sendEvent, reportError) { // preDeliver - the local router has identified the node it is going to send to. At this point, the message has been cloned if needed. hooks.trigger("preDeliver",sendEvent,(err) => { @@ -827,15 +838,10 @@ function handlePreDeliver(flow,sendEvent, reportError) { } else if (err !== false) { if (asyncMessageDelivery) { setImmediate(function() { - if (sendEvent.destination.node) { - sendEvent.destination.node.receive(sendEvent.msg); - } + deliverMessageToDestination(sendEvent) }) } else { - if (sendEvent.destination.node) { - sendEvent.destination.node.receive(sendEvent.msg); - - } + deliverMessageToDestination(sendEvent) } // postDeliver - the message has been dispatched to be delivered asynchronously (unless the sync delivery flag is set, in which case it would be continue as synchronous delivery) hooks.trigger("postDeliver", sendEvent, function(err) {