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

Add errorHandler tests

This commit is contained in:
Nick O'Leary 2015-02-20 19:54:19 +00:00
parent 5599b999ec
commit c80a44933c
3 changed files with 139 additions and 9 deletions

View File

@ -208,18 +208,19 @@ function diffNodeConfigs(oldNode,newNode) {
function createCatchNodeMap(nodes) { function createCatchNodeMap(nodes) {
var catchNodes = {}; var catchNodes = {};
var subflowInstances = {}; var subflowInstances = {};
var id;
/* /*
- a catchNode with same z as error node - a catchNode with same z as error node
- if error occurs on a subflow without catchNode, look at z of subflow instance - if error occurs on a subflow without catchNode, look at z of subflow instance
*/ */
for (var id in nodes) { for (id in nodes) {
if (nodes.hasOwnProperty(id)) { if (nodes.hasOwnProperty(id)) {
if (nodes[id].type === "catch") { if (nodes[id].type === "catch") {
catchNodes[nodes[id].z] = nodes[id]; catchNodes[nodes[id].z] = nodes[id];
} }
} }
} }
for (var id in nodes) { for (id in nodes) {
if (nodes.hasOwnProperty(id)) { if (nodes.hasOwnProperty(id)) {
var m = /^subflow:(.+)$/.exec(nodes[id].type); var m = /^subflow:(.+)$/.exec(nodes[id].type);
if (m) { if (m) {
@ -227,7 +228,7 @@ function createCatchNodeMap(nodes) {
} }
} }
} }
for (var id in subflowInstances) { for (id in subflowInstances) {
if (subflowInstances.hasOwnProperty(id)) { if (subflowInstances.hasOwnProperty(id)) {
var z = id; var z = id;
while(subflowInstances[z]) { while(subflowInstances[z]) {
@ -744,8 +745,8 @@ Flow.prototype.handleError = function(node,logMessage,msg) {
errorMessage.error = { errorMessage.error = {
message: logMessage.toString(), message: logMessage.toString(),
source: { source: {
id: this.id, id: node.id,
type: this.type type: node.type
} }
}; };
if (this.catchNodeMap[node.z]) { if (this.catchNodeMap[node.z]) {

View File

@ -574,9 +574,6 @@ describe('Flow', function() {
} }
util.inherits(TestNode,Node); util.inherits(TestNode,Node);
before(function() { before(function() {
getNode = sinon.stub(flows,"get",function(id) { getNode = sinon.stub(flows,"get",function(id) {
return currentNodes[id]; return currentNodes[id];
@ -809,9 +806,129 @@ describe('Flow', function() {
done(); done();
}); });
}); });
});
describe('#handleError',function() {
var getType;
var getNode;
var credentialsClean;
var currentNodes = {};
var TestNode = function(n) {
Node.call(this,n);
var node = this;
this.handled = [];
currentNodes[node.id] = node;
this.on('input',function(msg) {
node.handled.push(msg);
node.send(msg);
});
}
util.inherits(TestNode,Node);
before(function() {
getNode = sinon.stub(flows,"get",function(id) {
return currentNodes[id];
});
getType = sinon.stub(typeRegistry,"get",function(type) {
return TestNode;
});
credentialsClean = sinon.stub(credentials,"clean",function(config){});
});
after(function() {
getType.restore();
credentialsClean.restore();
getNode.restore();
});
beforeEach(function() {
currentNodes = {};
});
it("reports error to catch nodes on same z",function(done) {
var config = [
{id:"1",type:"test",z:"tab1",name:"a",wires:["2"]},
{id:"2",type:"catch",z:"tab1",wires:[[]]},
{id:"3",type:"catch",z:"tab2",wires:[[]]}
];
var flow = new Flow(config);
flow.start();
var msg = {a:1};
flow.handleError(getNode(1),"test error",msg);
var n2 = getNode(2);
n2.handled.should.have.lengthOf(1);
n2.handled[0].should.have.property("a",1);
n2.handled[0].should.have.property("error");
n2.handled[0].error.should.have.property("message","test error");
n2.handled[0].error.should.have.property("source");
n2.handled[0].error.source.should.have.property("id","1");
n2.handled[0].error.source.should.have.property("type","test");
getNode(3).handled.should.have.lengthOf(0);
done();
});
it("reports error with undefined message object",function(done) {
var config = [
{id:"1",type:"test",z:"tab1",name:"a",wires:["2"]},
{id:"2",type:"catch",z:"tab1",wires:[[]]},
{id:"3",type:"catch",z:"tab2",wires:[[]]}
];
var flow = new Flow(config);
flow.start();
flow.handleError(getNode(1),"test error");
var n2 = getNode(2);
n2.handled.should.have.lengthOf(1);
n2.handled[0].should.have.property("error");
n2.handled[0].error.should.have.property("message","test error");
n2.handled[0].error.should.have.property("source");
n2.handled[0].error.source.should.have.property("id","1");
n2.handled[0].error.source.should.have.property("type","test");
getNode(3).handled.should.have.lengthOf(0);
done();
});
it("reports error with Error object",function(done) {
var config = [
{id:"1",type:"test",z:"tab1",name:"a",wires:["2"]},
{id:"2",type:"catch",z:"tab1",wires:[[]]},
{id:"3",type:"catch",z:"tab2",wires:[[]]}
];
var flow = new Flow(config);
flow.start();
var msg = {a:1,error:"existing"};
flow.handleError(getNode(1),"test error",msg);
var n2 = getNode(2);
n2.handled.should.have.lengthOf(1);
n2.handled[0].should.have.property("error");
n2.handled[0].should.have.property("_error","existing");
n2.handled[0].error.should.have.property("message","test error");
n2.handled[0].error.should.have.property("source");
n2.handled[0].error.source.should.have.property("id","1");
n2.handled[0].error.source.should.have.property("type","test");
getNode(3).handled.should.have.lengthOf(0);
done();
});
it("reports error with Error object",function(done) {
var config = [
{id:"1",type:"test",z:"tab1",name:"a",wires:["2"]},
{id:"2",type:"catch",z:"tab1",wires:[[]]},
{id:"3",type:"catch",z:"tab2",wires:[[]]}
];
var flow = new Flow(config);
flow.start();
flow.handleError(getNode(1),new Error("test error"));
var n2 = getNode(2);
n2.handled.should.have.lengthOf(1);
n2.handled[0].should.have.property("error");
n2.handled[0].error.should.have.property("message","Error: test error");
n2.handled[0].error.should.have.property("source");
n2.handled[0].error.source.should.have.property("id","1");
n2.handled[0].error.source.should.have.property("type","test");
getNode(3).handled.should.have.lengthOf(0);
done();
});
}); });
}); });

View File

@ -385,10 +385,22 @@ describe('Node', function() {
sinon.stub(Log, 'log', function(msg) { sinon.stub(Log, 'log', function(msg) {
loginfo = msg; loginfo = msg;
}); });
n.error("an error message"); sinon.stub(flows,"handleError", function(node,message,msg) {
});
var message = {a:1};
n.error("an error message",message);
should.deepEqual({level:Log.ERROR, id:n.id, should.deepEqual({level:Log.ERROR, id:n.id,
type:n.type, msg:"an error message"}, loginfo); type:n.type, msg:"an error message"}, loginfo);
flows.handleError.called.should.be.true;
flows.handleError.args[0][0].should.eql(n);
flows.handleError.args[0][1].should.eql("an error message");
flows.handleError.args[0][2].should.eql(message);
Log.log.restore(); Log.log.restore();
flows.handleError.restore();
done(); done();
}); });
}); });