0
votes

My node-red is crashing multiple times per day. One of the problems I think that may be the cause is an http request I am doing.

I am requesting the output of a webpage in JSON. But sometimes I see in the log that returns an error in HTML. There is probably something wrong on the webpage serverside, but my flow is erroring out on this.

SyntaxError: undefined:1 <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> ^ Unexpected token <

2 Jan 15:26:26 - [error] [function:Filter temperatures] SyntaxError: undefined:1

Can I edit my functions to be able to filter this out, and hopefully not crash node-red any more?

This is an example of a function I am using to parse the JSON.

datarequest = JSON.parse(msg.payload);
msg1 = {};
msg1.payload = datarequest.data.valvesetat.bypass;


return [msg1];
2

2 Answers

1
votes

It looks like the web page is returning something that is not JSON (some kind of error, I guess) and the JSON.parse() fails to parse it.

Enclose the JSON.parse() part in a try/catch block and manage the response of the webpage.

0
votes

If there's no { characters in the HTML around the JSON, you could try:

msg.payload = msg.payload.replace(/^.*?(\{/{.*\}).*/, '$1');

before calling JSON.parse.