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.
This commit is contained in:
Samuel Reed 2015-01-08 22:07:23 +01:00
parent 30ff50c581
commit 30a7b6d9b8
2 changed files with 12 additions and 14 deletions

View File

@ -224,7 +224,7 @@ function runScripts(app, list, callback) {
var functions = []; var functions = [];
list.forEach(function(filepath) { list.forEach(function(filepath) {
debug('Requiring script %s', filepath); debug('Requiring script %s', filepath);
var exports = tryRequire(filepath); var exports = require(filepath);
if (typeof exports === 'function') { if (typeof exports === 'function') {
debug('Exported function detected %s', filepath); debug('Exported function detected %s', filepath);
functions.push({ functions.push({
@ -251,19 +251,6 @@ function runScripts(app, list, callback) {
}, 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) { function setupMiddleware(app, instructions) {
if (!instructions.middleware) { if (!instructions.middleware) {
// the browserified client does not support middleware // the browserified client does not support middleware

View File

@ -175,6 +175,17 @@ describe('executor', function() {
expect(app.models.Customer._modelsWhenAttached).to.include('UniqueName'); 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() { it('instantiates data sources', function() {
boot.execute(app, dummyInstructions); boot.execute(app, dummyInstructions);
assert(app.dataSources); assert(app.dataSources);