Node-RED guide series
Node-RED, the "catch" node
With the "catch" node, your flow can catch errors thrown by any node that belongs to the same flow.
The "catch" node is used for catching exceptions in your flow. Once the "catch" node catches an exception, it can pass the relevant information in the msg object to another node, such as a "debug" node, which can handle it.
All these, "catch" in Node-RED, "try" in Python, and "throw" in Ruby have the same purpose: to make it possible for your program to detect an exception condition and handle it gracefully instead of simply "crashing".
You can find the "catch" node under "common" in the left tool bar.
Setup the "catch" node
Let's experiment with the "catch" node.
We'll use the test flow from the previous article on "flows".
In the original flow, I have added two nodes: a "catch" node that monitors the flow for exceptions, and a "debug" node that will display information about an exception after it is caught.
The "catch" node is titled "Catch all errors".
The "debug" node is titled "Errors".
In the original flow "test" function, I have introduced an error. You should be able to see this error below:
The "catch" node only has two configuration options:
- catch errors from all nodes
- catch errors from selected nodes
When an error is caught, the "catch" node will store relevant information inside the msg object in the form of attached attributes:
You can see these options in the node's edit pane, and in the node information (documentation). I have included both of these in the screenshot below:
To display information about the error that I planted in the function node, we can use a debug node, configured like this:
When I deploy the flow and click on the inject node button, the function node will through an exception which is caught by the "catch" node. The "catch" node will pass information about the exception to the "debug" node which will show the error in the console, like this:
Now that I know there there is a bug, I can fix it and re-deploy the flow.
Learn Node-RED and how to use it with the ESP32
This course will guide you through the construction of an automated control system. The deliverable is a Terrarium controller.
Along the way, you will learn a great deal about useful technologies such as the Node Red programming environment and MQTT.
Jump to another article
1. Introduction to Node-RED
2. Install Node-RED on the Raspberry Pi
6. Messages and Variables
7. The "complete" node
8. The "catch" node
9. The "link out" and "link in" nodes
10. The "switch" node
11. The "range" node
12. The "delay" node
13. The "trigger" node
14. The "RBE" (Report by Exception) node