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,23 +24,24 @@ 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;
if (this.action === "change") {
try {
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) { this.on('input', function(msg) {
var propertyParts; var propertyParts;
var depth = 0; 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("."); propertyParts = node.property.split(".");
try { try {
propertyParts.reduce(function(obj, i) { propertyParts.reduce(function(obj, i) {
@ -86,5 +87,6 @@ module.exports = function(RED) {
node.send(msg); 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";
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"}); 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); 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);