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

Function node - handle things thrown that aren't proper Error types

and add tests
to close #2269
This commit is contained in:
Dave Conway-Jones 2019-08-19 10:42:14 +01:00
parent e72faef839
commit bf14af6a1f
No known key found for this signature in database
GPG Key ID: 9E7F9C73F5168CD4
2 changed files with 96 additions and 22 deletions

View File

@ -237,6 +237,7 @@ module.exports = function(RED) {
this.status({fill:"yellow",shape:"dot",text:""+converted}); this.status({fill:"yellow",shape:"dot",text:""+converted});
} }
} catch(err) { } catch(err) {
if ((typeof err === "object") && err.hasOwnProperty("stack")) {
//remove unwanted part //remove unwanted part
var index = err.stack.search(/\n\s*at ContextifyScript.Script.runInContext/); var index = err.stack.search(/\n\s*at ContextifyScript.Script.runInContext/);
err.stack = err.stack.slice(0, index).split('\n').slice(0,-1).join('\n'); err.stack = err.stack.slice(0, index).split('\n').slice(0,-1).join('\n');
@ -267,6 +268,13 @@ module.exports = function(RED) {
} }
this.error(errorMessage, msg); this.error(errorMessage, msg);
} }
else if (typeof err === "string") {
this.error(err, msg);
}
else {
this.error(JSON.stringify(err), msg);
}
}
}); });
this.on("close", function() { this.on("close", function() {
while (node.outstandingTimers.length > 0) { while (node.outstandingTimers.length > 0) {

View File

@ -1390,6 +1390,72 @@ describe('function node', function() {
} }
}); });
}); });
it('should catch thrown string', function (done) {
var flow = [{id: "n1", type: "function", wires: [["n2"]], func: "throw \"small mistake\";"}];
helper.load(functionNode, flow, function () {
var n1 = helper.getNode("n1");
n1.receive({payload: "foo", topic: "bar"});
try {
helper.log().called.should.be.true();
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', 'small mistake');
done();
} catch (err) {
done(err);
}
});
});
it('should catch thrown number', function (done) {
var flow = [{id: "n1", type: "function", wires: [["n2"]], func: "throw 99;"}];
helper.load(functionNode, flow, function () {
var n1 = helper.getNode("n1");
n1.receive({payload: "foo", topic: "bar"});
try {
helper.log().called.should.be.true();
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', '99');
done();
} catch (err) {
done(err);
}
});
});
it('should catch thrown object (bad practise)', function (done) {
var flow = [{id: "n1", type: "function", wires: [["n2"]], func: "throw {a:1};"}];
helper.load(functionNode, flow, function () {
var n1 = helper.getNode("n1");
n1.receive({payload: "foo", topic: "bar"});
try {
helper.log().called.should.be.true();
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', '{"a":1}');
done();
} catch (err) {
done(err);
}
});
});
}); });
}); });