Send error when payload is empty

I have flow, connected to PLC S7 and I collect data and send to MQTT, but when network is disconnected I not collect data.
I prepare function:

const x = msg.payload;

try{
   if(x == "") throw "empty";
      node.status({fill:"blue",shape:"dot",text:"on"});

msg.topic = "KIL4/SH";

let text = {};
let text1 = {};


         let mykey = "Flow_loopEdge"; 
        text[mykey] = "OK";
            
                    text1 = JSON.stringify([text]);
                   let obiekt = JSON.parse(text1);
        
    msg.payload = obiekt;
return msg;

}

catch(err){
    node.error(err)
     node.status({fill:"red",shape:"ring",text:"error"});
     
msg.topic = "KIL4/SH";

let text = {};
let text1 = {};


        let mykey = "Flow_loopEdge"; 
        text[mykey] = "NOK";
            
                    text1 = JSON.stringify([text]);
                   let obiekt = JSON.parse(text1);
        
    msg.payload = obiekt;
return msg;
}

but not send me error when payload is empty, I was wondering about using timeout.

node.error requires the 2nd parameter to be catchable.

node.error(err, msg);

Lastly, what is all that JSON.stringify / JSON.parse for?

Try this instead...

const mykey = "Flow_loopEdge";
msg.topic = "KIL4/SH";
try {
    if (msg.payload == "") throw "empty";
    node.status({ fill: "blue", shape: "dot", text: "on" });
    msg.payload = { [mykey] : "OK"};
} catch (err) {
    node.error(err, msg);
    node.status({ fill: "red", shape: "ring", text: "error" });
    msg.payload = { [mykey]: "NOK" };
}
return msg;

Thank you, your solution is good, but when someone disconnects wire I have last information, I would like, timeout send me NOK . This is possible?

You can use a Trigger node set to Send Nothing, then Wait For the appropriate time Then Send the NOK message. Also tick the Extend Delay if new message arrives.

The result is that all the time messages keep coming in within the timeout then the trigger node will send nothing. If no messages arrive during the timeout then it will send the message.

Can you show me where I should use?

[{"id":"44308971.15daa8","type":"mqtt out","z":"6073b3f4.c453cc","name":"","topic":"","qos":"","retain":"","broker":"752552c.ec79eac","x":1020,"y":160,"wires":[]},{"id":"1f67b87f.e8f508","type":"Datahub Subscribe","z":"6073b3f4.c453cc","name":"All Heating PLC data","natc":"42b5d73d.136d98","topicname":"devicehub.raw.B6070C84-DC01-4CCB-882E-CB6AF9E17C52.>","x":320,"y":160,"wires":[["521a54f5.c33cec","13c8e892.2c1f77"]]},{"id":"13c8e892.2c1f77","type":"debug","z":"6073b3f4.c453cc","name":"","active":false,"console":"false","complete":"false","x":600,"y":80,"wires":[]},{"id":"5f56bea8.94797","type":"debug","z":"6073b3f4.c453cc","name":"","active":false,"console":"false","complete":"false","x":1060,"y":100,"wires":[]},{"id":"707a2994.2dd7f8","type":"function","z":"6073b3f4.c453cc","name":"NewValue SH","func":"const temp = msg.payload.success;\nconst temp1 = msg.payload.tagName;\n\nif(temp && temp1) // Checks if the message succeeded and if the tag name exists before continuing.\n{\n   let datafilter = msg.payload;\n   let current = datafilter.value;\n  \n   let obj = datafilter.registerId;\n   let previous = context.get(obj);\n   \n   if (previous !== current)\n   {\n   context.set(obj,current);\n   node.status({fill:\"blue\",shape:\"dot\",text: (obj, current)});\n   return msg;\n   }\n}","outputs":1,"noerr":0,"x":605,"y":160,"wires":[["5229b4e1.41878c","60ad2094.0598b","73bd8497.0bef8c"]]},{"id":"521a54f5.c33cec","type":"json","z":"6073b3f4.c453cc","name":"JSON to Object","property":"payload","action":"obj","pretty":false,"x":480,"y":160,"wires":[["707a2994.2dd7f8"]]},{"id":"5229b4e1.41878c","type":"function","z":"6073b3f4.c453cc","name":"NewName_SH","func":" const myObj = msg.payload;\n const text = myObj.tagName;\n    \n\t\tif(text.startsWith(\"0#\") == true){\n\t\t\n\t\t\t//dla wartości 0#\n\t\t\t    const restText = text.substr(\"0#\".length);\n\t\t    myObj.tagName = restText;\n\t\t\tmyObj.value = myObj.value/1;\n\t\t\tmsg.payload = myObj;\n\t\t    return msg;\n\t\t\t}\n        else if(text.startsWith(\"1#\") == true){\n\t\t\n\t\t\t//dla wartości 1# - dzielenie wartości przez 10\n\t\t    \tconst restText = text.substr(\"1#\".length);\n\t\t    myObj.tagName = restText;\n\t\t\tmyObj.value = myObj.value/10;\n\t\t\tmsg.payload = myObj;\n\t\t    return msg;\n\t\t\t}\n\t\telse if(text.startsWith(\"2#\") == true){\n\t\t\n\t\t\t//dla wartości 2# - dzielenie wartości przez 100\n\t\t    \tconst restText = text.substr(\"2#\".length);\n\t\t    myObj.tagName = restText;\n\t\t\tmyObj.value = myObj.value/100;\n\t\t\tmsg.payload = myObj;\n\t\t    return msg;\n\t\t\t}\n\t\telse if(text.startsWith(\"3#\") == true){\n\t\t\n\t\t\t//dla wartości 3# - dzielenie wartości przez 1000\n\t\t    \tconst restText = text.substr(\"3#\".length);\n\t\t    myObj.tagName = restText;\n\t\t\tmyObj.value = myObj.value/1000;\n\t\t\tmsg.payload = myObj;\n\t\t    return msg;\n\t\t\t}\n\t\telse if(text.startsWith(\"#\", 1 ) == false){\n\t\t\n\t\t\t//brak przedrostka\n\t\t\tmsg.payload = myObj;\n\t\t\treturn msg;\n\t\t    }","outputs":1,"noerr":0,"x":765,"y":160,"wires":[["df9fe3bf.78676"]]},{"id":"df9fe3bf.78676","type":"function","z":"6073b3f4.c453cc","name":"TagTab","func":"let myObj = msg.payload;\nlet tmp = myObj.tagName;\n\n\nif(myObj.success && myObj.tagName)\n{\n    //aby wyswietlac dane tylko z danego sterownika do MQTT\nmsg.topic = \"KIL4/SH\";\n\n\nlet text = {};\nlet text1 = {};\n\n        \n        let mykey = myObj[\"tagName\"]; \n        text[mykey] = myObj[\"value\"];\n            \n                    text1 = JSON.stringify([text]);\n                   let obiekt = JSON.parse(text1);\n        \n    msg.payload = obiekt;\n    return msg;\n}","outputs":1,"noerr":0,"x":845,"y":160,"wires":[["44308971.15daa8","5f56bea8.94797"]]},{"id":"65dcd1c3.482c9","type":"comment","z":"6073b3f4.c453cc","name":"Sterownik Grzanie","info":"","x":720,"y":40,"wires":[]},{"id":"60ad2094.0598b","type":"function","z":"6073b3f4.c453cc","name":"","func":"const x = msg.payload;\n\ntry{\n   if(x == \"\") throw \"empty\";\n      node.status({fill:\"blue\",shape:\"dot\",text:\"on\"});\n\nmsg.topic = \"KIL4/SH\";\n\nlet text = {};\nlet text1 = {};\n\n\n         let mykey = \"Flow_loopEdge\"; \n        text[mykey] = \"OK\";\n            \n                    text1 = JSON.stringify([text]);\n                   let obiekt = JSON.parse(text1);\n        \n    msg.payload = obiekt;\nreturn msg;\n\n}\n\ncatch(err){\n    node.error(err)\n     node.status({fill:\"red\",shape:\"ring\",text:\"error\"});\n     \nmsg.topic = \"KIL4/SH\";\n\nlet text = {};\nlet text1 = {};\n\n\n        let mykey = \"Flow_loopEdge\"; \n        text[mykey] = \"NOK\";\n            \n                    text1 = JSON.stringify([text]);\n                   let obiekt = JSON.parse(text1);\n        \n    msg.payload = obiekt;\nreturn msg;\n}","outputs":1,"noerr":0,"x":800,"y":280,"wires":[["44308971.15daa8"]]},{"id":"73bd8497.0bef8c","type":"function","z":"6073b3f4.c453cc","name":"","func":"const mykey = \"Flow_loopEdge\";\nmsg.topic = \"KIL4/SH\";\ntry {\n    if (msg.payload == \"\") throw \"empty\";\n    node.status({ fill: \"blue\", shape: \"dot\", text: \"on\" });\n    msg.payload = { [mykey] : \"OK\"};\n} catch (err) {\n    node.error(err, msg);\n    node.status({ fill: \"red\", shape: \"ring\", text: \"error\" });\n    msg.payload = { [mykey]: \"NOK\" };\n}\nreturn msg;\n","outputs":1,"noerr":0,"x":845,"y":80,"wires":[["6e0a6a36.91cbf4"]]},{"id":"6e0a6a36.91cbf4","type":"debug","z":"6073b3f4.c453cc","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":1060,"y":40,"wires":[]},{"id":"752552c.ec79eac","type":"mqtt-broker","z":"","name":"","broker":"10.219.54.6","port":"8883","tls":"d9bf52f5.81cf4","clientid":"","usetls":true,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"42b5d73d.136d98","type":"Datahub Connect","z":"","ipadd":"127.0.0.1","port":"4222"},{"id":"d9bf52f5.81cf4","type":"tls-config","z":"","name":"MQTT","cert":"","key":"","ca":"","certname":"client-cert.pem","keyname":"client-key.pem","caname":"ca.pem","servername":"","verifyservercert":true}]

Configure a trigger node as I have described, connect an Inject node into it and a debug node on the output. Experiment with it to understand what it does. Then you should be able to work out what to do with it.