Add catch node

This commit is contained in:
Nick O'Leary
2015-02-20 01:17:24 +00:00
parent 172cbdaa84
commit 5599b999ec
5 changed files with 156 additions and 3 deletions

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,46 @@ function diffNodeConfigs(oldNode,newNode) {
return false;
}
function createCatchNodeMap(nodes) {
var catchNodes = {};
var subflowInstances = {};
/*
- a catchNode with same z as error node
- if error occurs on a subflow without catchNode, look at z of subflow instance
*/
for (var id in nodes) {
if (nodes.hasOwnProperty(id)) {
if (nodes[id].type === "catch") {
catchNodes[nodes[id].z] = nodes[id];
}
}
}
for (var id in nodes) {
if (nodes.hasOwnProperty(id)) {
var m = /^subflow:(.+)$/.exec(nodes[id].type);
if (m) {
subflowInstances[id] = nodes[id];
}
}
}
for (var 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 +272,8 @@ Flow.prototype.parseConfig = function(config) {
this.configNodes = {};
this.catchNodeMap = null;
var unknownTypes = {};
for (i=0;i<this.config.length;i++) {
@@ -349,6 +393,9 @@ Flow.prototype.start = function() {
}
}
}
this.catchNodeMap = createCatchNodeMap(this.activeNodes);
credentials.clean(this.config);
events.emit("nodes-started");
}
@@ -681,7 +728,34 @@ Flow.prototype.diffFlow = function(config) {
});
return diff;
}
Flow.prototype.handleError = function(node,logMessage,msg) {
var errorMessage;
if (typeof msg !== "undefined") {
errorMessage = redUtil.cloneMessage(msg);
} else {
errorMessage = {};
}
if (errorMessage.hasOwnProperty("error")) {
errorMessage._error = errorMessage.error;
}
errorMessage.error = {
message: logMessage.toString(),
source: {
id: this.id,
type: this.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,10 @@ 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);
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);
}
};