Search code examples
node-red

Is it possible to catch an object with the node.error() function on Node-RED?


Let's say I have the following flow:

enter image description here

The function node called error that we can catch but catch node doesn't see objects has the code:

const object = {
   "key1": "value1", 
   "key2": "value2"
}
node.error(object, msg)

If I run it the error will be caught by the catch node as follows:

enter image description here

Even though it catches the error, it only accepts strings and not objects when using node.error(object, msg). It'd work fine if not using the catch node... But when I use it I'll always receive [Object Object] in the catch node...


The node error that we cannot catch has the code:

const object = {
   "key1": "value1", 
   "key2": "value2"
}
msg.error = object

node.error(msg)

This node on the other hand prints the messages to the debug window but its messages are never caught by the catch node.

I'd like to make it in a way that I'll use the catch node to catch objects that I send as errors on my flow. Is it possible to do? Or the catch node doesn't support catching objects?

samples:

[{"id":"8bdb7d23cef69c8f","type":"catch","z":"e31af9f309041c23","name":"","scope":["31ef17186604a4ff","ac478768a62912b0"],"uncaught":false,"x":1390,"y":760,"wires":[["0dce50f2ce849b21"]]},{"id":"0dce50f2ce849b21","type":"debug","z":"e31af9f309041c23","name":"debug 1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1580,"y":760,"wires":[]},{"id":"31ef17186604a4ff","type":"function","z":"e31af9f309041c23","name":"error that we can catch but catch node doesn't see objects","currentLine":{"row":0,"column":0},"func":"const object = {\n   \"key1\": \"value1\", \n   \"key2\": \"value2\"\n}\nnode.error(object, msg)","outputs":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1730,"y":620,"wires":[]},{"id":"38ec456b9cfe72ae","type":"inject","z":"e31af9f309041c23","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1420,"y":620,"wires":[["31ef17186604a4ff"]]},{"id":"ac478768a62912b0","type":"function","z":"e31af9f309041c23","name":"error that we cannot catch","currentLine":{"row":0,"column":0},"func":"const object = {\n   \"key1\": \"value1\", \n   \"key2\": \"value2\"\n}\nmsg.error = object\n\nnode.error(msg)","outputs":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1630,"y":680,"wires":[]},{"id":"2c5f9396a10118b3","type":"inject","z":"e31af9f309041c23","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":1420,"y":680,"wires":[["ac478768a62912b0"]]}]

Solution

  • The first argument to node.error() should be a string, not an object. This is because it is explicitly meant to be a human readable message.

    Just do what you have done in the second instance, but include a text message to go with the error:

    const object = {
       "key1": "value1", 
       "key2": "value2"
    }
    msg.error = object
    
    node.error("Some error message",msg)