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

Log error when non-msg-object is returned from a Function

This commit is contained in:
Nick O'Leary 2017-05-15 13:54:05 +01:00
parent d4135e80a6
commit 8a7bb1be9f
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
3 changed files with 65 additions and 12 deletions

View File

@ -28,17 +28,22 @@ module.exports = function(RED) {
var msgCount = 0; var msgCount = 0;
for (var m=0;m<msgs.length;m++) { for (var m=0;m<msgs.length;m++) {
if (msgs[m]) { if (msgs[m]) {
if (util.isArray(msgs[m])) { if (!util.isArray(msgs[m])) {
msgs[m] = [msgs[m]];
}
for (var n=0; n < msgs[m].length; n++) { for (var n=0; n < msgs[m].length; n++) {
if (msgs[m][n] !== null && msgs[m][n] !== undefined) { var msg = msgs[m][n];
msgs[m][n]._msgid = _msgid; if (msg !== null && msg !== undefined) {
if (typeof msg === 'object' && !Buffer.isBuffer(msg) && !util.isArray(msg)) {
msg._msgid = _msgid;
msgCount++; msgCount++;
}
}
} else { } else {
if (msgs[m] !== null && msgs[m] !== undefined) { var type = typeof msg;
msgs[m]._msgid = _msgid; if (type === 'object') {
msgCount++; type = Buffer.isBuffer(msg)?'Buffer':(util.isArray(msg)?'Array':'Date');
}
node.error(RED._("function.error.non-message-returned",{ type: type }))
}
} }
} }
} }

View File

@ -169,7 +169,8 @@
"outputs": "Outputs" "outputs": "Outputs"
}, },
"error": { "error": {
"inputListener":"Cannot add listener to 'input' event within Function" "inputListener":"Cannot add listener to 'input' event within Function",
"non-message-returned":"Function tried to send a message of type __type__"
}, },
"tip": "See the Info tab for help writing functions." "tip": "See the Info tab for help writing functions."
}, },

View File

@ -127,7 +127,7 @@ describe('function node', function() {
var n2 = helper.getNode("n2"); var n2 = helper.getNode("n2");
setTimeout(function() { setTimeout(function() {
done(); done();
}, 200); }, 20);
n2.on("input", function(msg) { n2.on("input", function(msg) {
should.fail(null,null,"unexpected message"); should.fail(null,null,"unexpected message");
}); });
@ -156,9 +156,56 @@ describe('function node', function() {
n2MsgCount.should.equal(2); n2MsgCount.should.equal(2);
n3MsgCount.should.equal(0); n3MsgCount.should.equal(0);
done(); done();
},100); },20);
}); });
}); });
function testNonObjectMessage(functionText,done) {
var flow = [{id:"n1",type:"function",wires:[["n2"]],func:functionText},
{id:"n2", type:"helper"}];
helper.load(functionNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
var n2MsgCount = 0;
n2.on("input", function(msg) {
n2MsgCount++;
});
n1.receive({});
setTimeout(function() {
try {
n2MsgCount.should.equal(0);
var logEvents = helper.log().args.filter(function(evt) {
return evt[0].type == "function";
});
logEvents.should.have.length(1);
var msg = logEvents[0][0];
msg.should.have.property('level', helper.log().ERROR);
msg.should.have.property('id', 'n1');
msg.should.have.property('type', 'function');
msg.should.have.property('msg', 'function.error.non-message-returned');
done();
} catch(err) {
done(err);
}
},20);
});
}
it('should drop and log non-object message types - string', function(done) {
testNonObjectMessage('return "foo"', done)
});
it('should drop and log non-object message types - buffer', function(done) {
testNonObjectMessage('return new Buffer("hello")', done)
});
it('should drop and log non-object message types - array', function(done) {
testNonObjectMessage('return [[[1,2,3]]]', done)
});
it('should drop and log non-object message types - boolean', function(done) {
testNonObjectMessage('return true', done)
});
it('should drop and log non-object message types - number', function(done) {
testNonObjectMessage('return 123', done)
});
it('should handle and log script error', function(done) { it('should handle and log script error', function(done) {
var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"retunr"}]; var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"retunr"}];
helper.load(functionNode, flow, function() { helper.load(functionNode, flow, function() {