# Function node error checking

**URL:** https://discourse.nodered.org/t/function-node-error-checking/15170
**Category:** General
**Created:** [5 September 2019 19:10 UTC](https://discourse.nodered.org/t/function-node-error-checking/15170 "2019-09-05T19:10:48Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![m\_elias](https://avatars.discourse-cdn.com/v4/letter/m/94ad74/32.png) [@m\_elias](https://discourse.nodered.org/u/m_elias)
#### Post date: [5 September 2019 19:10 UTC](https://discourse.nodered.org/t/function-node-error-checking/15170/1 "2019-09-05T19:10:49Z")

</div>

I'm just learning this stuff as I go. I am parsing data returned by a HTTP GET input node and pub'ing each item's value to my mqtt server (thanks to bakman2)

> [@MQTT Pub each JSON value](https://discourse.nodered.org/t/mqtt-pub-each-json-value/13570):
>
> Hi, I'd like to publish several JSON object values from my weather station via MQTT. I am using a http request node to get the JSON object. First I started off using a change node to extract only one value but now I've decided it could be useful in the future to publish all of them. Is there a better approach than using a function node to loop through the array of objects, outputting to a mqtt output node for each value? Here's my array of objects, just the 2 so far. [{"macAddress":"XX:XX:XX:…

```auto
//obj = [{"macAddress":"XX:XX:XX:XX:XX:XX","lastData":{"dateutc":1563819660000,"winddir":206,"windspeedmph":0.45,"windgustmph":1.12,"maxdailygust":9.17,"tempf":81.3,"battout":1,"humidity":43,"hourlyrainin":0,"eventrainin":0,"dailyrainin":0,"weeklyrainin":0,"monthlyrainin":0.12,"yearlyrainin":0.12,"totalrainin":0.12,"tempinf":73,"battin":1,"humidityin":52,"baromrelin":29.37,"baromabsin":29.31,"uv":7,"solarradiation":750.41,"temp1f":69.26,"humidity1":58,"batt1":1,"feelsLike":81.2,"dewPoint":56.64,"lastRain":"2019-07-17T20:16:00.000Z","tz":"America/Chicago","date":"2019-07-22T18:21:00.000Z"},"info":{"name":"Name1","location":""}},{"macAddress":"XX:XX:XX:XX:XX:XX","lastData":{"dateutc":1563819660000,"winddir":266,"windspeedmph":3.36,"windgustmph":4.47,"maxdailygust":10.29,"tempf":77.5,"battout":1,"humidity":47,"hourlyrainin":0,"eventrainin":0,"dailyrainin":0,"weeklyrainin":0,"monthlyrainin":0.02,"yearlyrainin":0.02,"totalrainin":0.02,"uv":7,"solarradiation":789.92,"feelsLike":77.16,"dewPoint":55.66,"lastRain":"2019-07-19T01:07:00.000Z","tz":"America/Chicago","date":"2019-07-22T18:21:00.000Z"},"info":{"location":"","name":"Name2"}}]

for (let item in msg.payload){
    for (let key of Object.keys(msg.payload[item].lastData)){
      
        t = 'weather/' + msg.payload[item].info.name.toLowerCase() + "/" + key
        v = msg.payload[item].lastData[key]
        
        node.send({topic:t, payload:v})  
    }   
}

```

Periodically I get an error in my debug view from the function node.

```auto
TypeError: Cannot convert undefined or null to object

```

How can/should I go about catching and analyzing these errors?

---

<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: [5 September 2019 19:14 UTC](https://discourse.nodered.org/t/function-node-error-checking/15170/2 "2019-09-05T19:14:14Z")

</div>

I know the answer to your question but I’m pretty tired so excuses upfront if I write a bit incoherently. Beyond using node.send to send messages, you can use other functions on the same node object to log to the debug tab.  
See the logging part of this page of the docs.  
[https://nodered.org/docs/user-guide/writing-functions](https://nodered.org/docs/user-guide/writing-functions)  
Use that part as the equivalent to debugging nodes inside your function node. The error your get deals with something in that for loop, try to log the values to the debug window to figure out which caused it.  
My eyes are falling shut, if you need more help from me I might answer some 10 hours from now 🙂

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [5 September 2019 19:43 UTC](https://discourse.nodered.org/t/function-node-error-checking/15170/3 "2019-09-05T19:43:33Z")

</div>

To amplify the words in the writing functions link you can, of course, add data to node.warn(), so you can insert lines in your function like

```auto
node.warn(`item is: ${item}`)

```

---

<div class="post-metadata">

### Author: ![m\_elias](https://avatars.discourse-cdn.com/v4/letter/m/94ad74/32.png) [@m\_elias](https://discourse.nodered.org/u/m_elias)
#### Post date: [9 September 2019 13:46 UTC](https://discourse.nodered.org/t/function-node-error-checking/15170/4 "2019-09-09T13:46:38Z")

</div>

What I expect is happening is my http get is returning a corrupted or incomplete JSON which is not structured the way my function is expecting. I'd like to output that corrupt JSON to the debug but only when there is an error. I've added a catch node -\> debug output but I'm skeptical that it'll do anymore than output the same error message as I was getting before. Somehow I need to catch the error in the function node and output a node.error message that tells me where things went wrong with a copy of the faulty JSON.

---

<div class="post-metadata">

### Author: ![m\_elias](https://avatars.discourse-cdn.com/v4/letter/m/94ad74/32.png) [@m\_elias](https://discourse.nodered.org/u/m_elias)
#### Post date: [9 September 2019 13:47 UTC](https://discourse.nodered.org/t/function-node-error-checking/15170/5 "2019-09-09T13:47:29Z")

</div>

I'll play with some try..catch statements.

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [9 September 2019 14:50 UTC](https://discourse.nodered.org/t/function-node-error-checking/15170/6 "2019-09-09T14:50:05Z")

</div>

something like

```auto
try {
    var foo = JSON.parse(msg.payload);
    // etc , etc 
    // msg.payload = foo.after.some.processing
    return msg;
}
catch(e) {
    node.error("Error parsing: "+msg.payload , msg);
}

```

---

<div class="post-metadata">

### Author: ![m\_elias](https://avatars.discourse-cdn.com/v4/letter/m/94ad74/32.png) [@m\_elias](https://discourse.nodered.org/u/m_elias)
#### Post date: [13 September 2019 21:34 UTC](https://discourse.nodered.org/t/function-node-error-checking/15170/7 "2019-09-13T21:34:29Z")

</div>

I setup a catch node for the function in question and see that it includes the original msg object which kills my function. Looking at the data indicates that the API I'm connecting to thinks my API key is incorrect. This only sometimes happens a couple times a day so I think the remote API is just dropping the ball periodically and I'm going to ignore it for now.

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [14 September 2019 11:09 UTC](https://discourse.nodered.org/t/function-node-error-checking/15170/8 "2019-09-14T11:09:49Z")

</div>

That is a useful reminder that remote API's can and should never be considered reliable.

So the first thing you should always do when using one is to make sure that you are trapping or ignoring any errors.
