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

@ -0,0 +1,44 @@
<!--
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 that occur within flows on the same tab</p>
</script>
<script type="text/javascript">
RED.nodes.registerType('catch',{
category: 'input',
color:"#e49191",
defaults: {
name: {value:""}
},
inputs:0,
outputs:1,
icon: "arrow-in.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,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);
}
};