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

Merge pull request #571 from knolleary/errnode

Add Catch node
This commit is contained in:
Nick O'Leary 2015-02-26 22:58:03 +00:00
commit 971a62ebc9
7 changed files with 357 additions and 7 deletions

View File

@ -0,0 +1,62 @@
<!--
Copyright 2015 IBM Corp.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<script type="text/x-red" data-template-name="catch">
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="name">
</div>
</script>
<script type="text/x-red" data-help-name="catch">
<p>Catch errors thrown by nodes on the same tab.</p>
<p>If a node throws a error whilst handling a message, the flow will typically
halt. This node can be used to catch those errors and handle them with a
dedicated flow.</p>
<p>The node will catch errors thrown by any node on the same tab. If there
are multiple catch nodes on a tab, they will all get triggered.</p>
<p>If an error is thrown within a subflow, the error will get handled by any
catch nodes within the subflow. If none exists, the error is propagated
up to the tab the subflow instance is on.</p>
<p>The message sent by this node will be the original message if the node that
threw the error provided it. The message will have an <code>error</code>
property with the following attributes:
<ul>
<li><code>message</code> : the error message</li>
<li><code>source.id</code> : the id of the node that threw the error</li>
<li><code>source.type</code> : the type of the node that threw the error</li>
</ul>
</p>
<p>If the message already had a <code>error</code> property, it is copied to <code>_error</code>.</p>
</script>
<script type="text/javascript">
RED.nodes.registerType('catch',{
category: 'input',
color:"#e49191",
defaults: {
name: {value:""}
},
inputs:0,
outputs:1,
icon: "alert.png",
label: function() {
return this.name||"catch";
},
labelStyle: function() {
return this.name?"node_label_italic":"";
}
});
</script>

View File

@ -0,0 +1,29 @@
/**
* Copyright 2015 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
module.exports = function(RED) {
"use strict";
function CatchNode(n) {
RED.nodes.createNode(this,n);
var node = this;
this.on("input",function(msg) {
this.send(msg);
});
}
RED.nodes.registerType("catch",CatchNode);
}

View File

@ -60,6 +60,7 @@ function createSubflow(sf,sfn,subflows) {
node_map[node.id] = node;
node._alias = node.id;
node.id = nid;
node.z = sfn.id;
newNodes.push(node);
}
// Update all subflow interior wiring to reflect new node IDs
@ -80,6 +81,7 @@ function createSubflow(sf,sfn,subflows) {
var subflowInstance = {
id: sfn.id,
type: sfn.type,
z: sfn.z,
name: sfn.name,
wires: []
}
@ -203,6 +205,47 @@ function diffNodeConfigs(oldNode,newNode) {
return false;
}
function createCatchNodeMap(nodes) {
var catchNodes = {};
var subflowInstances = {};
var id;
/*
- a catchNode with same z as error node
- if error occurs on a subflow without catchNode, look at z of subflow instance
*/
for (id in nodes) {
if (nodes.hasOwnProperty(id)) {
if (nodes[id].type === "catch") {
catchNodes[nodes[id].z] = nodes[id];
}
}
}
for (id in nodes) {
if (nodes.hasOwnProperty(id)) {
var m = /^subflow:(.+)$/.exec(nodes[id].type);
if (m) {
subflowInstances[id] = nodes[id];
}
}
}
for (id in subflowInstances) {
if (subflowInstances.hasOwnProperty(id)) {
var z = id;
while(subflowInstances[z]) {
var sfi = subflowInstances[z];
if (!catchNodes[z]) {
z = sfi.z;
} else {
break;
}
}
if (catchNodes[z]) {
catchNodes[id] = catchNodes[z];
}
}
}
return catchNodes;
}
var subflowInstanceRE = /^subflow:(.+)$/;
@ -230,6 +273,8 @@ Flow.prototype.parseConfig = function(config) {
this.configNodes = {};
this.catchNodeMap = null;
var unknownTypes = {};
for (i=0;i<this.config.length;i++) {
@ -349,6 +394,9 @@ Flow.prototype.start = function() {
}
}
}
this.catchNodeMap = createCatchNodeMap(this.activeNodes);
credentials.clean(this.config);
events.emit("nodes-started");
}
@ -681,7 +729,34 @@ Flow.prototype.diffFlow = function(config) {
});
return diff;
}
Flow.prototype.handleError = function(node,logMessage,msg) {
var errorMessage;
if (msg) {
errorMessage = redUtil.cloneMessage(msg);
} else {
errorMessage = {};
}
if (errorMessage.hasOwnProperty("error")) {
errorMessage._error = errorMessage.error;
}
errorMessage.error = {
message: logMessage.toString(),
source: {
id: node.id,
type: node.type
}
};
if (this.catchNodeMap[node.z]) {
this.catchNodeMap[node.z].receive(errorMessage);
} else {
if (this.activeNodes[node.z] && this.catchNodeMap[this.activeNodes[node.z].z]) {
this.catchNodeMap[this.activeNodes[node.z].z].receive(errorMessage);
}
}
}
module.exports = Flow;

View File

@ -27,6 +27,7 @@ var comms = require("../comms");
function Node(n) {
this.id = n.id;
this.type = n.type;
this.z = n.z;
if (n.name) {
this.name = n.name;
}
@ -197,8 +198,12 @@ Node.prototype.warn = function(msg) {
log_helper(this, Log.WARN, msg);
};
Node.prototype.error = function(msg) {
log_helper(this, Log.ERROR, msg);
Node.prototype.error = function(logMessage,msg) {
logMessage = logMessage || "";
log_helper(this, Log.ERROR, logMessage);
if (msg) {
flows.handleError(this,logMessage,msg);
}
};
/**

View File

@ -147,6 +147,9 @@ var flowNodes = module.exports = {
log.info("Stopped flows");
return;
});
},
handleError: function(node,logMessage,msg) {
activeFlow.handleError(node,logMessage,msg);
}
};

View File

@ -574,9 +574,6 @@ describe('Flow', function() {
}
util.inherits(TestNode,Node);
before(function() {
getNode = sinon.stub(flows,"get",function(id) {
return currentNodes[id];
@ -809,9 +806,176 @@ describe('Flow', function() {
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 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();
});
it('reports error in subflow to a local handler', function(done) {
var config = [
{id:"1",type:"test",z:"tab1",wires:[[]]},
{id:"2",type:"subflow:sf1",z:"tab1",wires:[[]]},
{id:"3",type:"catch",z:"tab1",wires:[]},
{id:"sf1",type:"subflow","in": [],"out": []},
{id:"sf1-1",z:"sf1",type:"test",wires:[]},
{id:"sf1-catch",type:"catch",z:"sf1",wires:[]}
];
var flow = new Flow(config);
flow.start();
var instanceNode;
var instanceCatch;
for (var id in currentNodes) {
if (currentNodes.hasOwnProperty(id)) {
if (currentNodes[id].z == '2') {
if (currentNodes[id].type == "catch") {
instanceCatch = currentNodes[id];
} else {
instanceNode = currentNodes[id];
}
}
}
}
flow.handleError(instanceNode,new Error("test error"));
var n3 = instanceCatch;
n3.handled.should.have.lengthOf(1);
n3.handled[0].should.have.property("error");
n3.handled[0].error.should.have.property("message","Error: test error");
n3.handled[0].error.should.have.property("source");
n3.handled[0].error.source.should.have.property("id",instanceNode.id);
n3.handled[0].error.source.should.have.property("type","test");
getNode(3).handled.should.have.lengthOf(0);
done();
});
it('reports error in subflow to a parent handler', function(done) {
var config = [
{id:"1",type:"test",z:"tab1",wires:[[]]},
{id:"2",type:"subflow:sf1",z:"tab1",wires:[[]]},
{id:"3",type:"catch",z:"tab1",wires:[]},
{id:"4",type:"catch",z:"tab2",wires:[]},
{id:"sf1",type:"subflow","in": [],"out": []},
{id:"sf1-1",z:"sf1",type:"test",wires:[]}
];
var flow = new Flow(config);
flow.start();
var instanceNode;
for (var id in currentNodes) {
if (currentNodes.hasOwnProperty(id)) {
if (currentNodes[id].z == '2') {
instanceNode = currentNodes[id];
}
}
}
flow.handleError(instanceNode,new Error("test error"));
var n3 = getNode(3);
n3.handled.should.have.lengthOf(1);
n3.handled[0].should.have.property("error");
n3.handled[0].error.should.have.property("message","Error: test error");
n3.handled[0].error.should.have.property("source");
n3.handled[0].error.source.should.have.property("id",instanceNode.id);
n3.handled[0].error.source.should.have.property("type","test");
getNode(4).handled.should.have.lengthOf(0);
done();
});
});
});

View File

@ -385,10 +385,22 @@ describe('Node', function() {
sinon.stub(Log, 'log', function(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,
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();
flows.handleError.restore();
done();
});
});