6

I think, node.error does not halt the flow in all cases. My aim is to catch errors, save the payload and stop the flow.

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.

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.

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

Questions

How can I be sure that any node that raises a node.error() will definitely be caught by a Catch node? Or, in which circumstances, will the Catch node not catch a node.error()?

  • On the Postgres node, I have submitted a PR to raise errors that can be caught by a Catch node. – cogitoergosum Jul 9 '18 at 13:51
4

From the documentation:

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.

  • And, the flow halts regardless of whether node.error is called with one or two arguments? – cogitoergosum Jul 4 '18 at 10:48
  • 1
    Again, that will depend on the node, although it should stop the flow - but nothing would stop the node implementation from calling node.send after node.error and a message would be sent. – knolleary Jul 5 '18 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.