How to create an error message in a flow

I know this may seem weird, but I can't think of an easier way to solve the problem.

I have some function nodes which need things set in them for them to work.

If the variables aren't set, the nodes don't work. Not a big worry, but it is annoying remembering later that they aren't set and that's why it wasn't working.

What I am wanting to do is something like this:

var name = msg.payload.foo;
if (name *is undefined*)
{
    error_message = "node configuration not set";
    generate error();
}

I know it isn't syntactically correct, but I am sure you can get the idea.

This way my error trapping routines can catch the error and alert me of the problem, rather than having to set up an entirely new system to see that kind of thing.

My search on "how to create an error" was somewhat non productive - probably because I didn't word it correctly.

Thanks in advance.

https://www.w3schools.com/js/js_errors.asp

From that link:

The throw Statement

The throw statement allows you to create a custom error.

Technically you can throw an exception (throw an error) .

The exception can be a JavaScript String , a Number , a Boolean or an Object :

throw "Too big"; // throw a text
throw 500; // throw a number

Yields this test flow:

[{"id":"fe678007.8d0f5","type":"inject","z":"c191bc17.3b5c5","name":"TelePi","topic":"Test","payload":"foo","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":400,"y":1450,"wires":[["9745227d.83d5a8"]]},{"id":"9745227d.83d5a8","type":"function","z":"c191bc17.3b5c5","name":"","func":"throw \"this is a test error messages\";\n//return msg;","outputs":1,"noerr":0,"x":540,"y":1450,"wires":[[]]}]

And I get this error:

TypeError: Cannot read property 'search' of undefined

Yeah, real helpful.

I admit that originally I had the inject node (coppied from else where on the flow) had JS{ } stuff in it.
So I quickly changed it to just foo.

Same result when I press the node.

I do not really get your point, maybe because it works fine here

image

Shrug.

Beats me.

I don't get that error.

This is what I see - the big picture.

Your flow, just imported, works as expected. You must have some strange stuff in your system

Weird.

This is on the NUC.

Did it on a RPI and it works.

But the message is not as shown.
(Well it kinda works)

Update:
On the RPI that works, there is a catch node.
I see this when I look at the message:

{"_msgid":"44f8668b.b1e168","topic":"ERROR_REPORT/BedPi/Main Telemetry/Test","payload":"foo","error":"foo","time":"2019-9-22 16:02:29","ttl":0,"_queuetimestamp":1569132149174,"_queueCount":0}

And if I delete the catch node, I get what I did on the NUC.

Hmmmmm....

Note: The foo part is what the inject node injects.
I don't see the text this is a test error messages

Question that might be related, is there a difference in Node versions between the NUC and the Pi? I remember that in newer versions of Node you’re supposed to create an Error() object with the message inside, and throw that instead.

If you want a node to log an error then you use node.error("error message", msg). This is described in the docs here: https://nodered.org/docs/user-guide/writing-functions#handling-errors

2 Likes

In the Pi I tested it I have this version

pi@raspberrypi:~ $ node -v
v10.16.3

Thanks (again) Nick.

I tried searching - as said - and didn't get that.