Node-red catch节点
2015-09-24 00:33
756 查看
catch用于捕获流程中的节点在处理message时抛出的异常,类似于js的try{}catch(){}。
catch节点的js和html文件源码分别如下:
catch.js注册input事件,在捕获的异常时,输出异常msg,如有错误,msg中会包含错误信息error,error下含有message,source.id和source.type等错误具体信息和位置;
catch.html
代码中common.label.name,this._(“catch.catch”)均使用了llocales/en-US/messages.json中的值
启动node-red工具测试catch节点,inject节点触发一次异常,function节点抛出异常,debug节点打印出catch捕获msg格式,流程图如下:
function节点调用throw抛出异常,代码如下:
deploy后,点击inject节点,在debug信息栏可以看到catch捕获到的错误,结果如下:
catch节点的js和html文件源码分别如下:
catch.js注册input事件,在捕获的异常时,输出异常msg,如有错误,msg中会包含错误信息error,error下含有message,source.id和source.type等错误具体信息和位置;
[code]module.exports = function(RED) { "use strict";//使用js严格模式 function CatchNode(n) { RED.nodes.createNode(this,n); var node = this; this.on("input",function(msg) { this.send(msg); }); } RED.nodes.registerType("catch",CatchNode); }
catch.html
[code]<script type="text/x-red" data-template-name="catch"> <div class="form-row"> <label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label> <input type="text" id="node-input-name" data-i18n="[placeholder]common.label.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||this._("catch.catch"); }, labelStyle: function() { return this.name?"node_label_italic":""; } }); </script>
代码中common.label.name,this._(“catch.catch”)均使用了llocales/en-US/messages.json中的值
启动node-red工具测试catch节点,inject节点触发一次异常,function节点抛出异常,debug节点打印出catch捕获msg格式,流程图如下:
function节点调用throw抛出异常,代码如下:
deploy后,点击inject节点,在debug信息栏可以看到catch捕获到的错误,结果如下:
相关文章推荐
- nodejs、npm、grunt——名词解释
- node.js基础教程
- LeetCode(49) Populating Next Right Pointers in Each Node I II
- mac使用nvm安装node进行多版本管理
- LintCode Remove Node in Binary Search Tree
- Node.js Cluster多进程负载测试
- [LeetCode]#19 Remove Nth Node From the End of list
- Node.js知识点详解(一)基础部分
- node.js框架express入门模板
- [LeetCode]Delete Node in a Linked List
- Grunt学习笔记之开发环境的搭建与创建一个基本的项目
- nodejs+express 初级安装测试
- 快速搭建 Node.js 开发环境
- nodejs爬虫简易实现和jquery的each方法
- nodejs显现events.js:72抛出错误
- LeetCode(38) Delete Node in a Linked List
- 执行start-dfs.sh后,datenode没有启动
- Node.js中的DNS模块
- 从零开始nodejs系列文章
- Node.js开发框架Express4.x