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

Better error reporting when module provides duplicate type

This commit is contained in:
Nick O'Leary 2018-01-15 23:20:20 +00:00
parent 543a2b9dc7
commit fd4fdb31b5
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
5 changed files with 68 additions and 9 deletions

View File

@ -117,7 +117,11 @@ function start() {
if (nodeErrors.length > 0) { if (nodeErrors.length > 0) {
log.warn("------------------------------------------------------"); log.warn("------------------------------------------------------");
for (i=0;i<nodeErrors.length;i+=1) { for (i=0;i<nodeErrors.length;i+=1) {
log.warn("["+nodeErrors[i].name+"] "+nodeErrors[i].err); if (nodeErrors[i].err.code === "type_already_registered") {
log.warn("["+nodeErrors[i].id+"] "+log._("server.type-already-registered",{type:nodeErrors[i].err.details.type,module: nodeErrors[i].err.details.moduleA}));
} else {
log.warn("["+nodeErrors[i].id+"] "+nodeErrors[i].err);
}
} }
log.warn("------------------------------------------------------"); log.warn("------------------------------------------------------");
} }

View File

@ -20,6 +20,7 @@
"errors-help": "Run with -v for details", "errors-help": "Run with -v for details",
"missing-modules": "Missing node modules:", "missing-modules": "Missing node modules:",
"node-version-mismatch": "Node module cannot be loaded on this version. Requires: __version__ ", "node-version-mismatch": "Node module cannot be loaded on this version. Requires: __version__ ",
"type-already-registered": "'__type__' already registered by module __module__",
"removing-modules": "Removing modules from config", "removing-modules": "Removing modules from config",
"added-types": "Added node types:", "added-types": "Added node types:",
"removed-types": "Removed node types:", "removed-types": "Removed node types:",

View File

@ -214,15 +214,15 @@ function loadNodeConfig(fileInfo) {
} else { } else {
var types = []; var types = [];
var regExp = /<script ([^>]*)data-template-name=['"]([^'"]*)['"]/gi; var regExp = /<script (?:[^>]*)data-template-name\s*=\s*['"]([^'"]*)['"]/gi;
var match = null; var match = null;
while ((match = regExp.exec(content)) !== null) { while ((match = regExp.exec(content)) !== null) {
types.push(match[2]); types.push(match[1]);
} }
node.types = types; node.types = types;
var langRegExp = /^<script[^>]* data-lang=['"](.+?)['"]/i; var langRegExp = /^<script[^>]* data-lang\s*=\s*['"](.+?)['"]/i;
regExp = /(<script[^>]* data-help-name=[\s\S]*?<\/script>)/gi; regExp = /(<script[^>]* data-help-name=[\s\S]*?<\/script>)/gi;
match = null; match = null;
var mainContent = ""; var mainContent = "";

View File

@ -69,7 +69,7 @@ function filterNodeInfo(n) {
r.module = n.module; r.module = n.module;
} }
if (n.hasOwnProperty("err")) { if (n.hasOwnProperty("err")) {
r.err = n.err.toString(); r.err = n.err;
} }
return r; return r;
} }
@ -182,11 +182,25 @@ function loadNodeConfigs() {
} }
function addNodeSet(id,set,version) { function addNodeSet(id,set,version) {
if (!set.err) {
set.types.forEach(function(t) {
if (nodeTypeToId.hasOwnProperty(t)) {
set.err = new Error("Type already registered");
set.err.code = "type_already_registered";
set.err.details = {
type: t,
moduleA: getNodeInfo(t).module,
moduleB: set.module
}
}
});
if (!set.err) { if (!set.err) {
set.types.forEach(function(t) { set.types.forEach(function(t) {
nodeTypeToId[t] = id; nodeTypeToId[t] = id;
}); });
} }
}
moduleNodes[set.module] = moduleNodes[set.module]||[]; moduleNodes[set.module] = moduleNodes[set.module]||[];
moduleNodes[set.module].push(set.name); moduleNodes[set.module].push(set.name);

View File

@ -72,7 +72,15 @@ describe("red/nodes/registry/registry",function() {
config: "configC", config: "configC",
types: [ "test-c","test-d"] types: [ "test-c","test-d"]
}; };
var testNodeSet3 = {
id: "test-module-2/test-name-3",
module: "test-module-2",
name: "test-name-3",
enabled: true,
loaded: false,
config: "configB",
types: [ "test-a","test-e"]
};
@ -199,6 +207,38 @@ describe("red/nodes/registry/registry",function() {
should.not.exist(typeRegistry.getTypeId("test-c")); should.not.exist(typeRegistry.getTypeId("test-c"));
}); });
it('doesnt add node set if type already exists', function() {
typeRegistry.init(settings);
typeRegistry.getNodeList().should.have.lengthOf(0);
typeRegistry.getModuleList().should.eql({});
should.not.exist(typeRegistry.getTypeId("test-e"));
typeRegistry.addNodeSet("test-module/test-name",testNodeSet1, "0.0.1");
typeRegistry.getNodeList().should.have.lengthOf(1);
should.exist(typeRegistry.getTypeId("test-a"));
typeRegistry.addNodeSet(testNodeSet3.id,testNodeSet3, "0.0.1");
typeRegistry.getNodeList().should.have.lengthOf(2);
// testNodeSet3 registers a duplicate test-a and unique test-e
// as test-a is a duplicate, test-e should not get registered
should.not.exist(typeRegistry.getTypeId("test-e"));
var testNodeSet3Result = typeRegistry.getNodeList()[1];
should.exist(testNodeSet3Result.err);
testNodeSet3Result.err.code.should.equal("type_already_registered");
testNodeSet3Result.err.details.type.should.equal("test-a");
testNodeSet3Result.err.details.moduleA.should.equal("test-module");
testNodeSet3Result.err.details.moduleB.should.equal("test-module-2");
//
// typeRegistry.addNodeSet("test-module/test-name-2",testNodeSet2WithError, "0.0.1");
//
// should.not.exist(typeRegistry.getTypeId("test-c"));
});
}); });
describe("#enableNodeSet", function() { describe("#enableNodeSet", function() {
@ -334,7 +374,7 @@ describe("red/nodes/registry/registry",function() {
enabled: true, enabled: true,
loaded: false, loaded: false,
config: "configB", config: "configB",
types: [ "test-a","test-b"] types: [ "test-c","test-d"]
}, "0.0.1"); }, "0.0.1");
typeRegistry.getNodeConfig("test-module/test-name-2").should.eql('configBHEtest-name-2LP'); typeRegistry.getNodeConfig("test-module/test-name-2").should.eql('configBHEtest-name-2LP');
typeRegistry.getAllNodeConfigs().should.eql('configAHEtest-nameLPconfigBHEtest-name-2LP'); typeRegistry.getAllNodeConfigs().should.eql('configAHEtest-nameLPconfigBHEtest-name-2LP');