# How to pickup an error in a function

**URL:** <https://discourse.nodered.org/t/how-to-pickup-an-error-in-a-function/92899>\
**Category:** General\
**Created:** [4 November 2024 10:47 UTC](https://discourse.nodered.org/t/how-to-pickup-an-error-in-a-function/92899 "2024-11-04T10:47:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sirhc](https://avatars.discourse-cdn.com/v4/letter/s/b77776/32.png) [@Sirhc](https://discourse.nodered.org/u/Sirhc)\
**Post date:** [4 November 2024 10:47 UTC](https://discourse.nodered.org/t/how-to-pickup-an-error-in-a-function/92899/1 "2024-11-04T10:47:42Z")

</div>

Hello

I have a function that communicates to a devices IP address.

If the device isn't on the network, the debug window shows this:

function : (error)  
request to IP\_ADDR failed, reason: connect EHOSTUNREACH IP\_ADDR

How do I pick this error up in the function itself or in a function after it so I can flag that the device isn't connected

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [4 November 2024 10:49 UTC](https://discourse.nodered.org/t/how-to-pickup-an-error-in-a-function/92899/2 "2024-11-04T10:49:41Z")

</div>

Hard to say with such little info but at a guess `try {} catch () {}` - 100% depends on async await or sync or promise based code.

---

<div class="post-metadata">

**Author:** ![Sirhc](https://avatars.discourse-cdn.com/v4/letter/s/b77776/32.png) [@Sirhc](https://discourse.nodered.org/u/Sirhc)\
**Post date:** [4 November 2024 11:05 UTC](https://discourse.nodered.org/t/how-to-pickup-an-error-in-a-function/92899/3 "2024-11-04T11:05:46Z")

</div>

> [@Steve-Mcl](#):
>
> try {} catch () {}

Sorry, does this go in the function node itself?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [4 November 2024 11:11 UTC](https://discourse.nodered.org/t/how-to-pickup-an-error-in-a-function/92899/4 "2024-11-04T11:11:47Z")

</div>

> [@Sirhc](#):
>
> does this go in the function node itself?

again ......

> [@Steve-Mcl](#):
>
> Hard to say with such little info

where is the error generated? show us the flow / code

---

<div class="post-metadata">

**Author:** ![Sirhc](https://avatars.discourse-cdn.com/v4/letter/s/b77776/32.png) [@Sirhc](https://discourse.nodered.org/u/Sirhc)\
**Post date:** [4 November 2024 22:13 UTC](https://discourse.nodered.org/t/how-to-pickup-an-error-in-a-function/92899/5 "2024-11-04T22:13:34Z")

</div>

> [@Steve-Mcl](#):
>
> try {} catch () {}

Thanks for the reply and patience.

I am utilising this package:

[https://github.com/bbreukelen/node-rainbird/blob/master/index.js](https://github.com/bbreukelen/node-rainbird/blob/master/index.js)

In the function node I have:

> var temp = new RainBirdClass(MY\_IP, MY\_PASS);  
> let temp1 = await temp.getActiveZones();  
> msg.payload = temp1;

However, if the controller is not on the network, I see the above mentioned error in debug, which looks like it's the error returned line 139 of index.js?

> Line 139: reject(err.message)

;

Instead of the function giving me an error in debug, I want to know that the controller is not on the network.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [4 November 2024 22:18 UTC](https://discourse.nodered.org/t/how-to-pickup-an-error-in-a-function/92899/6 "2024-11-04T22:18:29Z")

</div>

Open the function node, select the set-up tab, set output count to 2. This way, good messages go out of the top node, errors go out the bottom output.

```auto
const temp = new RainBirdClass(MY_IP, MY_PASS);
try {
  msg.payload = await temp.getActiveZones()
} catch (error) {
  msg.payload = "something went wrong"
  msg.error = error
  return [null, msg] // send to 2nd output 
}
return msg

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [4 November 2024 22:19 UTC](https://discourse.nodered.org/t/how-to-pickup-an-error-in-a-function/92899/7 "2024-11-04T22:19:22Z")

</div>

Alternatively, add a catch node and point it at the function.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [2 February 2025 22:20 UTC](https://discourse.nodered.org/t/how-to-pickup-an-error-in-a-function/92899/8 "2025-02-02T22:20:01Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
