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",{}); node.emit("input",{});
}, this.repeat ); }, this.repeat );
} else if (this.crontab) { } else if (this.crontab) {
if (cron) { if (RED.settings.verbose) { this.log("crontab = "+this.crontab); }
if (RED.settings.verbose) { this.log("crontab = "+this.crontab); } this.cronjob = new cron.CronJob(this.crontab,
this.cronjob = new cron.CronJob(this.crontab, function() {
function() { node.emit("input",{});
node.emit("input",{}); },
}, null,true);
null,true);
} else {
this.error("'cron' module not found");
}
} }
if (this.once) { if (this.once) {

View File

@ -59,7 +59,7 @@ module.exports = function(RED) {
node.send([null,null,msg]); node.send([null,null,msg]);
}); });
ex.on('error', function (code) { 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"); } 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+")"; errorMessage += " (line "+line+", col "+cha+")";
} }
} }
this.error(errorMessage); this.error(errorMessage, msg);
} }
}); });
} catch(err) { } catch(err) {

View File

@ -68,7 +68,8 @@ module.exports = function(RED) {
} }
node.port.write(payload,function(err,res) { node.port.write(payload,function(err,res) {
if (err) { 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() { client.on('error', function() {
node.log('connect failed'); node.error('connect failed',msg);
node.status({fill:"red",shape:"ring",text:"error"}); node.status({fill:"red",shape:"ring",text:"error"});
if (client) { client.end(); } if (client) { client.end(); }
}); });
client.on('timeout',function() { client.on('timeout',function() {
node.log('connect timeout'); node.warn('connect timeout');
if (client) { if (client) {
client.end(); client.end();
setTimeout(function() { setTimeout(function() {

View File

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

View File

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

View File

@ -75,7 +75,7 @@ module.exports = function(RED) {
msg.payload = ou; msg.payload = ou;
node.send(msg); 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 else if (typeof msg.payload == "string") { // convert CSV string to object
try { try {
@ -160,7 +160,7 @@ module.exports = function(RED) {
node.send(msg); // finally send the array 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."); } 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); node.send(msg);
} }
} catch (error) { } 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); msg.payload = JSON.parse(msg.payload);
node.send(msg); node.send(msg);
} }
catch(e) { node.error(e+ "\n"+msg.payload); } catch(e) { node.error(e.message,msg); }
} }
else if (typeof msg.payload === "object") { else if (typeof msg.payload === "object") {
if (!Buffer.isBuffer(msg.payload) ) { if (!Buffer.isBuffer(msg.payload) ) {

View File

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

View File

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

View File

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

View File

@ -40,7 +40,7 @@ module.exports = function(RED) {
} else if (msg.hasOwnProperty('delete')) { } else if (msg.hasOwnProperty('delete')) {
node.warn("Deprecated: please use specific delete option in config dialog."); node.warn("Deprecated: please use specific delete option in config dialog.");
fs.unlink(filename, function (err) { 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") { } else if (typeof msg.payload != "undefined") {
var data = msg.payload; 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 // 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, {encoding:"binary"}, function (err) {
fs.writeFile(filename, data, "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 (RED.settings.verbose) { node.log('wrote to file: '+filename); }
}); });
} }
else if (this.overwriteFile === "delete") { else if (this.overwriteFile === "delete") {
fs.unlink(filename, function (err) { 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); } 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 // 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, {encoding:"binary"}, function (err) {
fs.appendFile(filename, data, "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); } else if (RED.settings.verbose) { node.log('appended to file: '+filename); }
}); });
} }
@ -103,7 +103,7 @@ module.exports = function(RED) {
msg.filename = filename; msg.filename = filename;
fs.readFile(filename,options,function(err,data) { fs.readFile(filename,options,function(err,data) {
if (err) { if (err) {
node.error(err); node.error(err,msg);
msg.error = err; msg.error = err;
delete msg.payload; delete msg.payload;
} else { } else {

View File

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

View File

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

View File

@ -19,6 +19,9 @@ var should = require("should");
var changeNode = require("../../../../nodes/core/logic/15-change.js"); var changeNode = require("../../../../nodes/core/logic/15-change.js");
var helper = require("../../helper.js"); var helper = require("../../helper.js");
var Log = require("../../../../red/log.js");
describe('ChangeNode', function() { describe('ChangeNode', function() {
beforeEach(function(done) { 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"]]}, var flow = [{"id":"changeNode1","type":"change","action":"change","property":"payload","from":"\\+**+","to":"NUMBER","reg":true,"name":"changeNode","wires":[["helperNode1"]]},
{id:"helperNode1", type:"helper", wires:[]}]; {id:"helperNode1", type:"helper", wires:[]}];
helper.load(changeNode, flow, function() { helper.load(changeNode, flow, function() {
var changeNode1 = helper.getNode("changeNode1"); var logEvents = helper.log().args.filter(function (evt) {
var helperNode1 = helper.getNode("helperNode1"); 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();
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"});
}); });
}); });
}); });

View File

@ -140,7 +140,7 @@ describe('html node', function() {
logEvents.should.have.length(1); logEvents.should.have.length(1);
// Each logEvent is the array of args passed to the function. // Each logEvent is the array of args passed to the function.
logEvents[0][0].should.have.a.property('msg'); 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(); done();
} catch(err) { } catch(err) {

View File

@ -83,7 +83,8 @@ describe('JSON node', function() {
return evt[0].type == "json"; return evt[0].type == "json";
}); });
logEvents.should.have.length(1); 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(); done();
} catch(err) { } catch(err) {
done(err); done(err);