Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Error Handling Node-RED | Ahmad Fauzi Firmansyah

Trigger a flow when a node throws an error


1. Problem

You want to trigger a flow when a node throws an error.

2. Solution

Use the Catch node to receive the error and trigger a flow.

Example

Flows:

[{"id":"2bd6810d.e22ece","type":"catch","z":"fc046f99.4be08","name":"","scope":["2c94a22c.91012
e"],"uncaught":false,"x":130,"y":160,"wires":[["d16b9fac.8212a"]]},{"id":"2c94a22c.91012e","type":"
function","z":"fc046f99.4be08","name":"Throw Error","func":"node.error(\"an example error\", msg);
","outputs":1,"noerr":0,"x":310,"y":100,"wires":[[]]},{"id":"d16b9fac.8212a","type":"debug","z":"fc04
6f99.4be08","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"er
ror","targetType":"msg","x":300,"y":160,"wires":[]},{"id":"c5ee9670.5dbbd8","type":"inject","z":"fc0
46f99.4be08","name":"Trigger
error","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDela
y":0.1,"x":110,"y":100,"wires":[["2c94a22c.91012e"]]}]

3. Discussion

The Catch node can be configured to catch errors from specific nodes in the flow or from any node.
This allows you to create different error handling flows for different nodes.

The Catch node sends on the message that was logged with the error. It also sets msg.error with details
of the error and which node triggered it.

Note that this requires nodes to properly log their errors so that they can be caught..
Error Handling Node-RED | Ahmad Fauzi Firmansyah

Automatically retry an action after an error


1. Problem

You want to retry an action after an error is thrown.

2. Solution

Use the Catch node to receive the error and connect it back to the node that needs to retry the action.

Example

Flows:

[{"id":"27e61f12.c1a15","type":"inject","z":"fc046f99.4be08","name":"","topic":"","payload":"","payl
oadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":100,"y":320,"wires":[["d7
d08440.31b678"]]},{"id":"d7d08440.31b678","type":"function","z":"fc046f99.4be08","name":"Rando
m error","func":"// Randomly throw an error rather than\n// pass on message.\nif (Math.random() <
0.5) {\n node.error(\"a random error\", msg);\n} else {\n return
msg;\n}","outputs":1,"noerr":0,"x":320,"y":320,"wires":[["f22b1e9a.5d89b"]]},{"id":"f22b1e9a.5d89b
","type":"debug","z":"fc046f99.4be08","name":"","active":true,"tosidebar":true,"console":false,"tost
atus":false,"complete":"false","x":510,"y":320,"wires":[]},{"id":"2166290d.98d736","type":"delay","z
":"fc046f99.4be08","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"
1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"sec
onds","drop":false,"x":240,"y":380,"wires":[["d7d08440.31b678"]]},{"id":"139b836e.7950ed","type":
"catch","z":"fc046f99.4be08","name":"","scope":["d7d08440.31b678"],"uncaught":false,"x":90,"y":3
80,"wires":[["2166290d.98d736","9c8ab214.0ecaa"]]},{"id":"9c8ab214.0ecaa","type":"debug","z":"fc
046f99.4be08","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"
error","targetType":"msg","x":240,"y":440,"wires":[]}]

3. Discussion

Some errors are transitory and an action simply needs to be retried in order to succeed. Alternatively,
there may be some remedial action needed before retrying.

In the example flow, a Function simulates a random error - there is a 50% chance it will throw an error
rather than pass on the message.

The Catch receives the error which passes the message back to the Function node to retry. It also
includes a Delay node as, in some circumstances, it is suitable to wait for a short interval before
retrying..

You might also like