Skip to content

Commit

Permalink
Log error when non-msg-object is returned from a Function
Browse files Browse the repository at this point in the history
  • Loading branch information
knolleary committed May 15, 2017
1 parent d4135e8 commit 8a7bb1b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 12 deletions.
23 changes: 14 additions & 9 deletions nodes/core/core/80-function.js
Expand Up @@ -28,18 +28,23 @@ module.exports = function(RED) {
var msgCount = 0;
for (var m=0;m<msgs.length;m++) {
if (msgs[m]) {
if (util.isArray(msgs[m])) {
for (var n=0; n < msgs[m].length; n++) {
if (msgs[m][n] !== null && msgs[m][n] !== undefined) {
msgs[m][n]._msgid = _msgid;
if (!util.isArray(msgs[m])) {
msgs[m] = [msgs[m]];
}
for (var n=0; n < msgs[m].length; n++) {
var msg = msgs[m][n];
if (msg !== null && msg !== undefined) {
if (typeof msg === 'object' && !Buffer.isBuffer(msg) && !util.isArray(msg)) {
msg._msgid = _msgid;
msgCount++;
} else {
var type = typeof msg;
if (type === 'object') {
type = Buffer.isBuffer(msg)?'Buffer':(util.isArray(msg)?'Array':'Date');
}
node.error(RED._("function.error.non-message-returned",{ type: type }))
}
}
} else {
if (msgs[m] !== null && msgs[m] !== undefined) {
msgs[m]._msgid = _msgid;
msgCount++;
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion nodes/core/locales/en-US/messages.json
Expand Up @@ -169,7 +169,8 @@
"outputs": "Outputs"
},
"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."
},
Expand Down
51 changes: 49 additions & 2 deletions test/nodes/core/core/80-function_spec.js
Expand Up @@ -127,7 +127,7 @@ describe('function node', function() {
var n2 = helper.getNode("n2");
setTimeout(function() {
done();
}, 200);
}, 20);
n2.on("input", function(msg) {
should.fail(null,null,"unexpected message");
});
Expand Down Expand Up @@ -156,9 +156,56 @@ describe('function node', function() {
n2MsgCount.should.equal(2);
n3MsgCount.should.equal(0);
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) {
var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"retunr"}];
helper.load(functionNode, flow, function() {
Expand Down

0 comments on commit 8a7bb1b

Please sign in to comment.