Function node error checking

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)

//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.

TypeError: Cannot convert undefined or null to object

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

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
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 :slight_smile:

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

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

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.

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

something like

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);
}

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.

1 Like

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.