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

update handling of invalid jsonata expression

This commit is contained in:
Hiroyasu Nishiyama 2020-05-11 14:51:47 +09:00
parent d6ad7dc6eb
commit 00e080459e
4 changed files with 81 additions and 3 deletions

View File

@ -45,6 +45,19 @@ module.exports = function(RED) {
this.cronjob = null;
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) {
node.error(RED._("inject.errors.toolong", this));
delete node.repeat;
@ -86,10 +99,22 @@ module.exports = function(RED) {
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 {
RED.util.setMessageProperty(msg,property,RED.util.evaluateNodeProperty(value, valueType, this, msg),true);
} catch (err) {
errors.push(err);
errors.push(err.toString());
}
});

View File

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

View File

@ -83,7 +83,8 @@
"success": "inject処理を実行しました: __label__",
"errors": {
"failed": "inject処理が失敗しました。詳細はログを確認してください。",
"toolong": "時間間隔が大き過ぎます"
"toolong": "時間間隔が大き過ぎます",
"invalid-expr": "JSONata式が不正: __error__"
}
},
"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() {
it('should inject message', function(done) {
helper.load(injectNode,