From 7042d874443ab225030349cabb902c680bc3b992 Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Mon, 18 Aug 2014 21:59:19 +0100 Subject: [PATCH] Handle missing file/modules in Add node api --- red/nodes/registry.js | 5 +++++ test/red/nodes/registry_spec.js | 35 ++++++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/red/nodes/registry.js b/red/nodes/registry.js index 3133d8982..936c99839 100644 --- a/red/nodes/registry.js +++ b/red/nodes/registry.js @@ -466,6 +466,11 @@ function addNode(options) { } } else if (options.module) { var moduleFiles = scanTreeForNodesModules(options.module); + if (moduleFiles.length === 0) { + var err = new Error("Cannot find module '" + options.module + "'"); + err.code = 'MODULE_NOT_FOUND'; + return when.reject(err); + } moduleFiles.forEach(function(moduleFile) { nodes = nodes.concat(loadNodesFromModule(moduleFile.dir,moduleFile.package)); }); diff --git a/test/red/nodes/registry_spec.js b/test/red/nodes/registry_spec.js index a41fa4865..48c178d9f 100644 --- a/test/red/nodes/registry_spec.js +++ b/test/red/nodes/registry_spec.js @@ -310,6 +310,22 @@ describe('NodeRegistry', function() { }); }); + it('fails to add non-existent filename', function(done) { + typeRegistry.init({}); + typeRegistry.load("wontexist",true).then(function(){ + var list = typeRegistry.getNodeList(); + list.should.be.an.Array.and.be.empty; + typeRegistry.addNode({file: resourcesDir + "DoesNotExist/DoesNotExist.js"}).then(function(node) { + done(new Error("ENOENT not thrown")); + }).otherwise(function(e) { + e.code.should.eql("ENOENT"); + done(); + }); + + }).catch(function(e) { + done(e); + }); + }); it('returns node info by type or id', function(done) { typeRegistry.init({}); @@ -501,7 +517,6 @@ describe('NodeRegistry', function() { }); })(); - typeRegistry.init({}); typeRegistry.load("wontexist",true).then(function(){ var list = typeRegistry.getNodeList(); @@ -537,6 +552,24 @@ describe('NodeRegistry', function() { }); }); + it('fails to add non-existent module name', function(done) { + typeRegistry.init({}); + typeRegistry.load("wontexist",true).then(function(){ + var list = typeRegistry.getNodeList(); + list.should.be.an.Array.and.be.empty; + + typeRegistry.addNode({module: "DoesNotExistModule"}).then(function(node) { + done(new Error("ENOENT not thrown")); + }).otherwise(function(e) { + e.code.should.eql("MODULE_NOT_FOUND"); + done(); + }); + + }).catch(function(e) { + done(e); + }); + }); + it('allows nodes to be enabled and disabled', function(done) { typeRegistry.init({});