1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Merge pull request #3911 from node-red/protect-hooks

Ensure errors in preDeliver callback are handled
This commit is contained in:
Nick O'Leary 2023-02-21 14:26:39 +00:00 committed by GitHub
commit 6044871438
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -818,6 +818,16 @@ function handlePreRoute(flow, sendEvent, reportError) {
}) })
} }
function deliverMessageToDestination(sendEvent) {
if (sendEvent?.destination?.node) {
try {
sendEvent.destination.node.receive(sendEvent.msg);
} catch(err) {
Log.error(`Error delivering message to node:${sendEvent.destination.node._path} [${sendEvent.destination.node.type}]`)
Log.error(err.stack)
}
}
}
function handlePreDeliver(flow,sendEvent, reportError) { 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. // 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) => { hooks.trigger("preDeliver",sendEvent,(err) => {
@ -827,15 +837,10 @@ function handlePreDeliver(flow,sendEvent, reportError) {
} else if (err !== false) { } else if (err !== false) {
if (asyncMessageDelivery) { if (asyncMessageDelivery) {
setImmediate(function() { setImmediate(function() {
if (sendEvent.destination.node) { deliverMessageToDestination(sendEvent)
sendEvent.destination.node.receive(sendEvent.msg);
}
}) })
} else { } else {
if (sendEvent.destination.node) { deliverMessageToDestination(sendEvent)
sendEvent.destination.node.receive(sendEvent.msg);
}
} }
// 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) // 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) { hooks.trigger("postDeliver", sendEvent, function(err) {