# How to create an error message in a flow

**URL:** <https://discourse.nodered.org/t/how-to-create-an-error-message-in-a-flow/15764>\
**Category:** General\
**Created:** [22 September 2019 03:56 UTC](https://discourse.nodered.org/t/how-to-create-an-error-message-in-a-flow/15764 "2019-09-22T03:56:08Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [22 September 2019 03:56 UTC](https://discourse.nodered.org/t/how-to-create-an-error-message-in-a-flow/15764/1 "2019-09-22T03:56:08Z")

</div>

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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [22 September 2019 04:29 UTC](https://discourse.nodered.org/t/how-to-create-an-error-message-in-a-flow/15764/2 "2019-09-22T04:29:25Z")

</div>

[https://www.w3schools.com/js/js\_errors.asp](https://www.w3schools.com/js/js_errors.asp)

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [22 September 2019 04:39 UTC](https://discourse.nodered.org/t/how-to-create-an-error-message-in-a-flow/15764/3 "2019-09-22T04:39:20Z")

</div>

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:

```auto
[{"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_:

```auto
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.

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [22 September 2019 05:00 UTC](https://discourse.nodered.org/t/how-to-create-an-error-message-in-a-flow/15764/4 "2019-09-22T05:00:56Z")

</div>

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

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/8/8e51878786f9f288a2ef9e2ba1cf48160d1161cc.png)

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [22 September 2019 05:50 UTC](https://discourse.nodered.org/t/how-to-create-an-error-message-in-a-flow/15764/5 "2019-09-22T05:50:32Z")

</div>

Shrug.

Beats me.

I don't get that error.

This is what I see - the big picture.

 ![Screenshot%20from%202019-09-22%2015-49-10](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/0/0e346718518b7aac07ff1f268cf83fe9d6444d9f.png)

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [22 September 2019 05:56 UTC](https://discourse.nodered.org/t/how-to-create-an-error-message-in-a-flow/15764/6 "2019-09-22T05:56:08Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/d/df046c845cd5f9a955419f8d51ca3e73ee21026c.png)

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [22 September 2019 06:05 UTC](https://discourse.nodered.org/t/how-to-create-an-error-message-in-a-flow/15764/7 "2019-09-22T06:05:16Z")

</div>

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:

```auto
{"_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`

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [22 September 2019 06:52 UTC](https://discourse.nodered.org/t/how-to-create-an-error-message-in-a-flow/15764/8 "2019-09-22T06:52:33Z")

</div>

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.

---

<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:** [22 September 2019 07:06 UTC](https://discourse.nodered.org/t/how-to-create-an-error-message-in-a-flow/15764/9 "2019-09-22T07:06:16Z")

</div>

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](https://nodered.org/docs/user-guide/writing-functions#handling-errors)

---

<div class="post-metadata">

**Author:** ![krambriw](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/krambriw/32/5429_2.png) [@krambriw](https://discourse.nodered.org/u/krambriw)\
**Post date:** [22 September 2019 07:28 UTC](https://discourse.nodered.org/t/how-to-create-an-error-message-in-a-flow/15764/10 "2019-09-22T07:28:21Z")

</div>

In the Pi I tested it I have this version

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

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [22 September 2019 07:57 UTC](https://discourse.nodered.org/t/how-to-create-an-error-message-in-a-flow/15764/11 "2019-09-22T07:57:27Z")

</div>

Thanks (again) Nick.

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