From 30a7b6d9b88c45993f0728b1fd624d4032f2fb46 Mon Sep 17 00:00:00 2001 From: Samuel Reed Date: Thu, 8 Jan 2015 22:07:23 +0100 Subject: [PATCH] Don't swallow error when a sub-dependency doesn't resolve. This prevents an occurence where an error is completely swallowed if a script required by loopback-boot has a bad require() call. The script is never ran but execution continues. --- lib/executor.js | 15 +-------------- test/executor.test.js | 11 +++++++++++ 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/lib/executor.js b/lib/executor.js index 0e151d5..3e4c53d 100644 --- a/lib/executor.js +++ b/lib/executor.js @@ -224,7 +224,7 @@ function runScripts(app, list, callback) { var functions = []; list.forEach(function(filepath) { debug('Requiring script %s', filepath); - var exports = tryRequire(filepath); + var exports = require(filepath); if (typeof exports === 'function') { debug('Exported function detected %s', filepath); functions.push({ @@ -251,19 +251,6 @@ function runScripts(app, list, callback) { }, callback); } -function tryRequire(modulePath) { - try { - return require.apply(this, arguments); - } catch (e) { - if (e.code === 'MODULE_NOT_FOUND') { - debug('Warning: cannot require %s - module not found.', modulePath); - return undefined; - } - console.error('failed to require "%s"', modulePath); - throw e; - } -} - function setupMiddleware(app, instructions) { if (!instructions.middleware) { // the browserified client does not support middleware diff --git a/test/executor.test.js b/test/executor.test.js index 2330266..8fd6f86 100644 --- a/test/executor.test.js +++ b/test/executor.test.js @@ -175,6 +175,17 @@ describe('executor', function() { expect(app.models.Customer._modelsWhenAttached).to.include('UniqueName'); }); + it('throws on bad require() call inside boot script', function() { + var file = appdir.writeFileSync('boot/badScript.js', + 'require("doesnt-exist"); module.exports = {};'); + + function doBoot() { + boot.execute(app, someInstructions({ files: { boot: [file] } })); + } + + expect(doBoot).to.throw(/Cannot find module \'doesnt-exist\'/); + }); + it('instantiates data sources', function() { boot.execute(app, dummyInstructions); assert(app.dataSources);