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.

The "catch" node is similar to Python's "try" statement or Ruby's "throw" statements.

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.

The "catch" node.

Setup the "catch" node

Let's experiment with the "catch" node.

We'll use the test flow from the previous article on "flows".

The "Catch" node can detect exceptions anywhere in the flow.

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:

Ooops... I've made a typo. Can you find it?

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:

  • error.message
  • error.source.type

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:

"catch" node configuration and output.

To display information about the error that I planted in the function node, we can use a debug node, configured like this:

Display the contents of msg.error.message

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:

Oops, there's the bug.

Now that I know there there is a bug, I can fix it and re-deploy the flow.

All good!

It's good practice to keep a "catch" node in your flows just in case the JavaScript in the function nodes contain typos or errors that, otherwise, will not be reported.

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.

Browse this article
{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}