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

Merge pull request #2561 from node-red-hitachi/inject-node-error-handling

Update inject node error handling
This commit is contained in:
Nick O'Leary 2020-05-11 09:37:43 +01:00 committed by GitHub
commit 9a17cc503c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 81 additions and 3 deletions

View File

@ -45,6 +45,19 @@ module.exports = function(RED) {
this.cronjob = null; this.cronjob = null;
var node = this; var node = this;
node.props.forEach(function (prop) {
if (prop.vt === "jsonata") {
try {
var val = prop.v ? prop.v : "";
prop.exp = RED.util.prepareJSONataExpression(val, node);
}
catch (err) {
node.error(RED._("inject.errors.invalid-expr", {error:err.message}));
prop.exp = null;
}
}
});
if (node.repeat > 2147483) { if (node.repeat > 2147483) {
node.error(RED._("inject.errors.toolong", this)); node.error(RED._("inject.errors.toolong", this));
delete node.repeat; delete node.repeat;
@ -86,10 +99,22 @@ module.exports = function(RED) {
if (!property) return; if (!property) return;
if (valueType === "jsonata") {
if (p.exp) {
try {
var val = RED.util.evaluateJSONataExpression(p.exp, msg);
RED.util.setMessageProperty(msg, property, val, true);
}
catch (err) {
errors.push(err.message);
}
}
return;
}
try { try {
RED.util.setMessageProperty(msg,property,RED.util.evaluateNodeProperty(value, valueType, this, msg),true); RED.util.setMessageProperty(msg,property,RED.util.evaluateNodeProperty(value, valueType, this, msg),true);
} catch (err) { } catch (err) {
errors.push(err); errors.push(err.toString());
} }
}); });

View File

@ -83,7 +83,8 @@
"success": "Successfully injected: __label__", "success": "Successfully injected: __label__",
"errors": { "errors": {
"failed": "inject failed, see log for details", "failed": "inject failed, see log for details",
"toolong": "Interval too large" "toolong": "Interval too large",
"invalid-expr": "Invalid JSONata expression: __error__"
} }
}, },
"catch": { "catch": {

View File

@ -83,7 +83,8 @@
"success": "inject処理を実行しました: __label__", "success": "inject処理を実行しました: __label__",
"errors": { "errors": {
"failed": "inject処理が失敗しました。詳細はログを確認してください。", "failed": "inject処理が失敗しました。詳細はログを確認してください。",
"toolong": "時間間隔が大き過ぎます" "toolong": "時間間隔が大き過ぎます",
"invalid-expr": "JSONata式が不正: __error__"
} }
}, },
"catch": { "catch": {

View File

@ -488,6 +488,57 @@ describe('inject node', function() {
}); });
}); });
it('should inject multiple properties ', function (done) {
var flow = [{id: "n1", type: "inject", props: [{p:"topic", v:"t1", vt:"str"}, {p:"payload", v:"foo", vt:"str"}, {p:"x", v: 10, "vt":"num"}, {p:"y", v: "x+2", "vt":"jsonata"}], wires: [["n2"]], z: "flow"},
{id: "n2", type: "helper"}];
helper.load(injectNode, flow, function () {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function (msg) {
try {
msg.should.have.property("topic", "t1");
msg.should.have.property("payload", "foo");
msg.should.have.property("x", 10);
msg.should.have.property("y", 12);
done();
} catch (err) {
done(err);
}
});
n1.receive({});
});
});
it('should report invalid JSONata expression', function (done) {
var flow = [{id: "n1", type: "inject", props: [{p:"topic", v:"t1", vt:"str"}, {p:"payload", v:"@", vt:"jsonata"}], wires: [["n2"]], z: "flow"},
{id: "n2", type: "helper"}];
helper.load(injectNode, flow, function () {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
var count = 0;
n2.on("input", function (msg) {
try {
msg.should.have.property("topic", "t1");
msg.should.not.have.property("payload");
count++;
if (count == 2) {
done();
}
} catch (err) {
done(err);
}
});
n1.on("call:error", function(err) {
count++;
if (count == 2) {
done();
}
});
n1.receive({});
});
});
describe('post', function() { describe('post', function() {
it('should inject message', function(done) { it('should inject message', function(done) {
helper.load(injectNode, helper.load(injectNode,