Add try/catch to daemon

This commit is contained in:
Dave Conway-Jones 2018-02-09 12:51:42 +00:00
parent 3b20b804ae
commit bb3d302bcb
No known key found for this signature in database
GPG Key ID: 9E7F9C73F5168CD4
2 changed files with 52 additions and 43 deletions

View File

@ -36,15 +36,16 @@ module.exports = function(RED) {
}
function runit() {
var line = "";
if (!node.cmd || (typeof node.cmd !== "string") || (node.cmd.length < 1)) {
node.status({fill:"grey",shape:"ring",text:"no command"});
return;
}
try {
node.child = spawn(node.cmd, node.args);
if (RED.settings.verbose) { node.log(node.cmd+" "+JSON.stringify(node.args)); }
node.status({fill:"green",shape:"dot",text:"running"});
node.running = true;
var line = "";
node.child.stdout.on('data', function (data) {
if (node.op === "string") { data = data.toString(); }
@ -86,9 +87,17 @@ module.exports = function(RED) {
if (err.errno === "ENOENT") { node.warn('Command not found'); }
else if (err.errno === "EACCES") { node.warn('Command not executable'); }
else { node.log('error: ' + err); }
node.status({fill:"grey",shape:"dot",text:"error"});
node.status({fill:"red",shape:"ring",text:"error"});
});
}
catch(e) {
if (e.errno === "ENOENT") { node.warn('Command not found'); }
else if (e.errno === "EACCES") { node.warn('Command not executable'); }
else { node.error(e); }
node.status({fill:"red",shape:"ring",text:"error"});
node.running = false;
}
}
if (node.redo === true) {
var loop = setInterval( function() {

View File

@ -1,6 +1,6 @@
{
"name" : "node-red-node-daemon",
"version" : "0.0.17",
"version" : "0.0.18",
"description" : "A Node-RED node that runs and monitors a long running system command.",
"dependencies" : {
},