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.
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);
};