# Node.error() not caught in Catch node

**URL:** <https://discourse.nodered.org/t/node-error-not-caught-in-catch-node/1301>\
**Category:** General\
**Created:** [4 July 2018 04:09 UTC](https://discourse.nodered.org/t/node-error-not-caught-in-catch-node/1301 "2018-07-04T04:09:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![chainhead](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chainhead/32/40800_2.png) [@chainhead](https://discourse.nodered.org/u/chainhead)\
**Post date:** [4 July 2018 04:09 UTC](https://discourse.nodered.org/t/node-error-not-caught-in-catch-node/1301/1 "2018-07-04T04:09:40Z")

</div>

By default, will an error raised by `node.error()` be caught by Catch node. If not, why? Or, in which circumstances, will the Catch node _not_ catch a `node.error()`?

Please refer the observations and experiments below.

# Observation

**Function node** - Neither error is caught nor does the flow halt.

**JSON node** - Error can be caught and flow halts.

**Postgres node** - Error is not caught but the flow halts.

# Experiments

All of the experiments below have Catch nodes to handle errors from all nodes in the flow.

## Function node

I added a function node that is wired to an Inject node. The output of this node is a database `INSERT` query, wired to a Postgres node. In the function node, right at the top, I added this `node.error('Test error')`. While the error can be seen in the console, I can see row inserted into the database. I am not able to catch it either.

## JSON node

I wired a JSON node to a MQTT Input node to transform the JSON string payload to a JSON object. This object is passed to a function that generates a `INSERT` query statement and is wired to a Postgres node. For testing, I passed a malformed JSON document which causes an error in the JSON node. I can catch it and the flow stops too.

## Postgres node

With good JSON document, no `node.error()` statement in function node, the Postgres node inserts successfully. For testing, I created a well-formed JSON document that will raise a `duplicate on insert` message. In this case, I can see the error message in console. But, I can’t catch it.

Note that, the Postgres node does raise a `node.error()`.

### Query execution

Refer [this](https://github.com/BruceFletcher/node-red-contrib-postgres-multi/blob/f6e8758984179e3db8b483ef8843a1648096bf1d/postgres.js#L124).

```
try {
            for (let i=0; i < queries.length; ++i) {
              const { query, params = {}, output = false } = queries[i];
              const result = await client.query(query, params);

              if (output && node.output) {
                outMsg.payload = outMsg.payload.concat(result.rows);
              }
            }

            if (node.output) {
              node.send(outMsg);
            }
          } catch(e) {
            handleError(e, msg);
          } finally {
            client.release();
}

```

### Error handling

Refer [this](https://github.com/BruceFletcher/node-red-contrib-postgres-multi/blob/f6e8758984179e3db8b483ef8843a1648096bf1d/postgres.js#L109).

```
var handleError = (err, msg) => {
        node.error(err);
        console.log(err);
        console.log(msg.payload);
      };

```

---

<div class="post-metadata">

**Author:** ![chainhead](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chainhead/32/40800_2.png) [@chainhead](https://discourse.nodered.org/u/chainhead)\
**Post date:** [4 July 2018 04:12 UTC](https://discourse.nodered.org/t/node-error-not-caught-in-catch-node/1301/2 "2018-07-04T04:12:13Z")

</div>

This question was originally asked at [SO](https://iot.stackexchange.com/questions/3138/node-red-node-error-does-not-stop-flow-sometimes). I am adding a reply as only two hyper-links can be added by a newbie. 🙄

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [4 July 2018 06:13 UTC](https://discourse.nodered.org/t/node-error-not-caught-in-catch-node/1301/3 "2018-07-04T06:13:32Z")

</div>

From the [documentation](https://nodered.org/docs/creating-nodes/node-js#handling-errors):

> If the error is one that a user of the node may want to handle for themselves, the function should be called with the original message (or an empty message if this is an Input node) as the second argument:

> node.error("hit an error", msg);

> This will trigger any Catch nodes present on the same tab.

So calling `node.error()` with one argument will not trigger a Catch node.
