1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Add node.error handling to core nodes

This commit is contained in:
Nick O'Leary 2015-03-16 13:58:01 +00:00
parent 5efc89d514
commit be61cf6a88
19 changed files with 107 additions and 110 deletions

View File

@ -37,16 +37,12 @@ module.exports = function(RED) {
node.emit("input",{});
}, this.repeat );
} else if (this.crontab) {
if (cron) {
if (RED.settings.verbose) { this.log("crontab = "+this.crontab); }
this.cronjob = new cron.CronJob(this.crontab,
function() {
node.emit("input",{});
},
null,true);
} else {
this.error("'cron' module not found");
}
if (RED.settings.verbose) { this.log("crontab = "+this.crontab); }
this.cronjob = new cron.CronJob(this.crontab,
function() {
node.emit("input",{});
},
null,true);
}
if (this.once) {

View File

@ -59,7 +59,7 @@ module.exports = function(RED) {
node.send([null,null,msg]);
});
ex.on('error', function (code) {
node.error(code);
node.error(code,msg);
});
}
else { node.error("Spawn command must be just the command - no spaces or extra parameters"); }

View File

@ -90,7 +90,7 @@ module.exports = function(RED) {
errorMessage += " (line "+line+", col "+cha+")";
}
}
this.error(errorMessage);
this.error(errorMessage, msg);
}
});
} catch(err) {

View File

@ -68,7 +68,8 @@ module.exports = function(RED) {
}
node.port.write(payload,function(err,res) {
if (err) {
node.error(err);
var errmsg = err.toString().replace("Serialport","Serialport "+node.port.serial.path);
node.error(errmsg,msg);
}
});
});

View File

@ -470,13 +470,13 @@ module.exports = function(RED) {
});
client.on('error', function() {
node.log('connect failed');
node.error('connect failed',msg);
node.status({fill:"red",shape:"ring",text:"error"});
if (client) { client.end(); }
});
client.on('timeout',function() {
node.log('connect timeout');
node.warn('connect timeout');
if (client) {
client.end();
setTimeout(function() {

View File

@ -154,7 +154,7 @@ module.exports = function(RED) {
}
sock.send(message, 0, message.length, por, add, function(err, bytes) {
if (err) {
node.error("udp : "+err);
node.error("udp : "+err,msg);
}
message = null;
});

View File

@ -24,67 +24,69 @@ module.exports = function(RED) {
this.from = n.from || "";
this.to = n.to || "";
this.reg = (n.reg === null || n.reg);
var node = this;
if (node.reg === false) {
if (this.reg === false) {
this.from = this.from.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}
this.on('input', function(msg) {
var propertyParts;
var depth = 0;
if (node.action === "change") {
try {
node.re = new RegExp(this.from, "g");
} catch (e) {
node.error(e.message);
}
}
propertyParts = node.property.split(".");
var valid = true;
if (this.action === "change") {
try {
propertyParts.reduce(function(obj, i) {
var to = node.to;
// Set msg from property to another msg property
if (node.action === "replace" && node.to.indexOf("msg.") === 0) {
var parts = to.substring(4);
var msgPropParts = parts.split(".");
try {
msgPropParts.reduce(function(ob, j) {
to = (typeof ob[j] !== "undefined" ? ob[j] : undefined);
return to;
}, msg);
} catch (err) {}
}
if (++depth === propertyParts.length) {
if (node.action === "change") {
if (typeof obj[i] === "string") {
obj[i] = obj[i].replace(node.re, node.to);
}
} else if (node.action === "replace") {
if (typeof to === "undefined") {
this.re = new RegExp(this.from, "g");
} catch (e) {
valid = false;
this.error("Invalid 'from' property: "+e.message);
}
}
if (valid) {
var node = this;
this.on('input', function(msg) {
var propertyParts;
var depth = 0;
propertyParts = node.property.split(".");
try {
propertyParts.reduce(function(obj, i) {
var to = node.to;
// Set msg from property to another msg property
if (node.action === "replace" && node.to.indexOf("msg.") === 0) {
var parts = to.substring(4);
var msgPropParts = parts.split(".");
try {
msgPropParts.reduce(function(ob, j) {
to = (typeof ob[j] !== "undefined" ? ob[j] : undefined);
return to;
}, msg);
} catch (err) {}
}
if (++depth === propertyParts.length) {
if (node.action === "change") {
if (typeof obj[i] === "string") {
obj[i] = obj[i].replace(node.re, node.to);
}
} else if (node.action === "replace") {
if (typeof to === "undefined") {
delete(obj[i]);
} else {
obj[i] = to;
}
} else if (node.action === "delete") {
delete(obj[i]);
} else {
obj[i] = to;
}
} else if (node.action === "delete") {
delete(obj[i]);
} else {
// to property doesn't exist, don't create empty object
if (typeof to === "undefined") {
return;
// setting a non-existent multilevel object, create empty parent
} else if (!obj[i]) {
obj[i] = {};
}
return obj[i];
}
} else {
// to property doesn't exist, don't create empty object
if (typeof to === "undefined") {
return;
// setting a non-existent multilevel object, create empty parent
} else if (!obj[i]) {
obj[i] = {};
}
return obj[i];
}
}, msg);
} catch (err) {}
node.send(msg);
});
}, msg);
} catch (err) {}
node.send(msg);
});
}
}
RED.nodes.registerType("change", ChangeNode);
};

View File

@ -75,7 +75,7 @@ module.exports = function(RED) {
msg.payload = ou;
node.send(msg);
}
catch(e) { node.error(e); }
catch(e) { node.error(e,msg); }
}
else if (typeof msg.payload == "string") { // convert CSV string to object
try {
@ -160,7 +160,7 @@ module.exports = function(RED) {
node.send(msg); // finally send the array
}
}
catch(e) { node.error(e); }
catch(e) { node.error(e,msg); }
}
else { node.warn("This node only handles csv strings or js objects."); }
}

View File

@ -52,7 +52,7 @@ module.exports = function(RED) {
node.send(msg);
}
} catch (error) {
node.error('Error: '+error.message);
node.error(error.message,msg);
}
});
}

View File

@ -28,7 +28,7 @@ module.exports = function(RED) {
msg.payload = JSON.parse(msg.payload);
node.send(msg);
}
catch(e) { node.error(e+ "\n"+msg.payload); }
catch(e) { node.error(e.message,msg); }
}
else if (typeof msg.payload === "object") {
if (!Buffer.isBuffer(msg.payload) ) {

View File

@ -33,7 +33,7 @@ module.exports = function(RED) {
}
else if (typeof msg.payload == "string") {
parseString(msg.payload, {strict:true,async:true,attrkey:node.attrkey,charkey:node.charkey}, function (err, result) {
if (err) { node.error(err); }
if (err) { node.error(err, msg); }
else {
msg.payload = result;
node.send(msg);

View File

@ -296,13 +296,13 @@ module.exports = function(RED) {
var r = request.post(signedUrl,function(err,httpResponse,body) {
if (err) {
node.error(err.toString());
node.error(err,msg);
node.status({fill:"red",shape:"ring",text:"failed"});
} else {
var response = JSON.parse(body);
if (response.errors) {
var errorList = response.errors.map(function(er) { return er.code+": "+er.message }).join(", ");
node.error("tweet failed: "+errorList);
node.error("Send tweet failed: "+errorList,msg);
node.status({fill:"red",shape:"ring",text:"failed"});
} else {
node.status({});
@ -317,7 +317,7 @@ module.exports = function(RED) {
twit.updateStatus(msg.payload, function (err, data) {
if (err) {
node.status({fill:"red",shape:"ring",text:"failed"});
node.error(err);
node.error(err,msg);
}
node.status({});
});

View File

@ -93,7 +93,7 @@ module.exports = function(RED) {
}
smtpTransport.sendMail(sendopts, function(error, info) {
if (error) {
node.error(error);
node.error(error,msg);
node.status({fill:"red",shape:"ring",text:"send failed"});
} else {
node.log("Message sent: " + info.response);

View File

@ -40,7 +40,7 @@ module.exports = function(RED) {
} else if (msg.hasOwnProperty('delete')) {
node.warn("Deprecated: please use specific delete option in config dialog.");
fs.unlink(filename, function (err) {
if (err) { node.error('Failed to delete file : '+err); }
if (err) { node.error('Failed to delete file : '+err,msg); }
});
} else if (typeof msg.payload != "undefined") {
var data = msg.payload;
@ -53,13 +53,13 @@ module.exports = function(RED) {
// using "binary" not {encoding:"binary"} to be 0.8 compatible for a while
//fs.writeFile(filename, data, {encoding:"binary"}, function (err) {
fs.writeFile(filename, data, "binary", function (err) {
if (err) { node.error('Failed to write to file : '+err); }
if (err) { node.error('Failed to write to file : '+err,msg); }
else if (RED.settings.verbose) { node.log('wrote to file: '+filename); }
});
}
else if (this.overwriteFile === "delete") {
fs.unlink(filename, function (err) {
if (err) { node.error('Failed to delete file : '+err); }
if (err) { node.error('Failed to delete file : '+err,msg); }
else if (RED.settings.verbose) { node.log("deleted file: "+filename); }
});
}
@ -67,7 +67,7 @@ module.exports = function(RED) {
// using "binary" not {encoding:"binary"} to be 0.8 compatible for a while longer
//fs.appendFile(filename, data, {encoding:"binary"}, function (err) {
fs.appendFile(filename, data, "binary", function (err) {
if (err) { node.error('Failed to append to file : '+err); }
if (err) { node.error('Failed to append to file : '+err,msg); }
else if (RED.settings.verbose) { node.log('appended to file: '+filename); }
});
}
@ -103,7 +103,7 @@ module.exports = function(RED) {
msg.filename = filename;
fs.readFile(filename,options,function(err,data) {
if (err) {
node.error(err);
node.error(err,msg);
msg.error = err;
delete msg.payload;
} else {

View File

@ -76,7 +76,7 @@ module.exports = function(RED) {
if (msg.collection) {
coll = db.collection(msg.collection);
} else {
node.error("No collection defined");
node.error("No collection defined",msg);
return;
}
}
@ -89,13 +89,13 @@ module.exports = function(RED) {
}
coll.save(msg.payload,function(err, item) {
if (err) {
node.error(err);
node.error(err,msg);
}
});
} else {
coll.save(msg,function(err, item) {
if (err) {
node.error(err);
node.error(err,msg);
}
});
}
@ -106,13 +106,13 @@ module.exports = function(RED) {
}
coll.insert(msg.payload, function(err, item) {
if (err) {
node.error(err);
node.error(err,msg);
}
});
} else {
coll.insert(msg, function(err,item) {
if (err) {
node.error(err);
node.error(err,msg);
}
});
}
@ -129,13 +129,13 @@ module.exports = function(RED) {
coll.update(query, payload, options, function(err, item) {
if (err) {
node.error(err + " " + payload);
node.error(err,msg);
}
});
} else if (node.operation === "delete") {
coll.remove(msg.payload, function(err, items) {
if (err) {
node.error(err);
node.error(err,msg);
}
});
}

View File

@ -44,7 +44,7 @@ function init(_settings,storage) {
function needsPermission(permission) {
return function(req,res,next) {
if (settings.adminAuth) {
if (settings && settings.adminAuth) {
return passport.authenticate(['bearer','anon'],{ session: false })(req,res,function() {
if (!req.user) {
return next();

View File

@ -19,6 +19,9 @@ var should = require("should");
var changeNode = require("../../../../nodes/core/logic/15-change.js");
var helper = require("../../helper.js");
var Log = require("../../../../red/log.js");
describe('ChangeNode', function() {
beforeEach(function(done) {
@ -335,21 +338,15 @@ describe('ChangeNode', function() {
var flow = [{"id":"changeNode1","type":"change","action":"change","property":"payload","from":"\\+**+","to":"NUMBER","reg":true,"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1");
var helperNode1 = helper.getNode("helperNode1");
sinon.stub(changeNode1, 'error', function(error) {
if(error.indexOf("regular expression" > -1)) {
done();
} else {
try {
should.fail(null, null, "An error should be reported for an invalid regex");
} catch (err) {
done(err);
}
}
});
changeNode1.receive({payload:"This is irrelevant"});
var logEvents = helper.log().args.filter(function (evt) {
return evt[0].type == "change";
});
logEvents.should.have.length(1);
var msg = logEvents[0][0];
msg.should.have.property('level', helper.log().ERROR);
msg.should.have.property('id', 'changeNode1');
done();
});
});
});

View File

@ -140,7 +140,7 @@ describe('html node', function() {
logEvents.should.have.length(1);
// Each logEvent is the array of args passed to the function.
logEvents[0][0].should.have.a.property('msg');
logEvents[0][0].msg.should.startWith("Error:");
logEvents[0][0].should.have.a.property('level',helper.log().ERROR);
done();
} catch(err) {

View File

@ -83,7 +83,8 @@ describe('JSON node', function() {
return evt[0].type == "json";
});
logEvents.should.have.length(1);
logEvents[0][0].should.have.a.property('msg',"SyntaxError: Unexpected token o"+ "\nfoo");
logEvents[0][0].should.have.a.property('msg',"Unexpected token o");
logEvents[0][0].should.have.a.property('level',helper.log().ERROR);
done();
} catch(err) {
done(err);