Skip to content

Commit

Permalink
Merge pull request #32 from losandes/andes/shifting-deps
Browse files Browse the repository at this point in the history
Fixes Resolve When Modules Throw Errors
  • Loading branch information
losandes committed Aug 22, 2017
2 parents 61610de + fe4f01c commit 3d0bc94
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 55 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "hilary",
"main": "release/hilary.js",
"version": "5.0.0",
"version": "5.0.1",
"homepage": "https://github.com/losandes/hilaryjs",
"authors": [
"&y <andes.collab@gmail.com> (https://github.com/losandes)"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hilary",
"version": "5.0.0",
"version": "5.0.1",
"description": "a simple Dependency Injection (DI) library that provides Inversion of Control (IoC) capabilities in Node.js and the browser",
"main": "index.js",
"scripts": {
Expand Down
42 changes: 26 additions & 16 deletions release-candidate/hilary.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! hilary 2017-07-11 */
/*! hilary 2017-08-22 */

(function(register) {
"use strict";
Expand All @@ -9,6 +9,7 @@
INVALID_ARG: "InvalidArgument",
INVALID_REGISTRATION: "InvalidRegistration",
MODULE_NOT_FOUND: "ModuleNotFound",
MODULE_NOT_RESOLVED: "ModuleNotResolved",
BOOTSTRAP_FAILED: "BootstrapFailed"
},
container: {
Expand All @@ -25,6 +26,7 @@
RESOLVE_ARG: "resolve expects a moduleName (string) as the first argument, but instead saw this: ",
MODULE_NOT_FOUND: 'The module, "{{module}}", cannot be found',
MODULE_NOT_FOUND_RELYING: ', and is a dependency of, "{{startingModule}}"',
MODULE_THREW: 'The module, "{{module}}", cannot be resolved because it returned or threw an Error',
REGISTRATION_BLACK_LIST: "A module was registered with a reserved name: ",
PARENT_CONTAINER_ARG: "setParentScope expects the name of the parent scope, or an instance of Hilary"
},
Expand Down Expand Up @@ -573,7 +575,7 @@
if (!dependency) {
logger.trace("the following dependency was not resolved: " + item);
return cb(null, dependencies, relyingModuleName);
} else if (dependency.isException) {
} else if (dependency.isException || dependency instanceof Error) {
logger.error("the following dependency returned an exception: " + item);
return cb(dependency);
}
Expand All @@ -595,15 +597,15 @@
});
return next(err);
}
ctx.resolved = invoke(ctx.theModule.factory, dependencies);
ctx.resolved = invoke(ctx.theModule.name, ctx.theModule.factory, dependencies);
ctx.registerSingleton = ctx.theModule.singleton;
ctx.isResolved = true;
logger.trace("dependencies resolved for: " + ctx.name);
next(null, ctx);
});
} else if (is.function(ctx.theModule.factory) && ctx.theModule.factory.length === 0) {
logger.trace("the factory is a function and takes no arguments, returning the result of executing it: " + ctx.name);
ctx.resolved = invoke(ctx.theModule.factory);
ctx.resolved = invoke(ctx.theModule.name, ctx.theModule.factory);
} else {
logger.trace("the factory takes arguments and has no dependencies, returning the function as-is: " + ctx.name);
ctx.resolved = ctx.theModule.factory;
Expand Down Expand Up @@ -664,6 +666,26 @@
});
}
};
function invoke(name, factory, args) {
try {
if (isConstructor(factory)) {
if (args) {
args = [ null ].concat(args);
} else {
args = [ null ];
}
return new (Function.prototype.bind.apply(factory, args))();
} else {
return factory.apply(null, args);
}
} catch (e) {
return new Exception({
type: locale.errorTypes.MODULE_NOT_RESOLVED,
messages: [ locale.api.MODULE_THREW.replace(/{{module}}/, name), e.message ],
error: e
});
}
}
}
function parseDependencyName(dependencyName) {
var memberMatches = /\{([^}]+)\}/.exec(dependencyName), members = [];
Expand Down Expand Up @@ -704,18 +726,6 @@
return window[moduleName];
}
}
function invoke(factory, args) {
if (isConstructor(factory)) {
if (args) {
args = [ null ].concat(args);
} else {
args = [ null ];
}
return new (Function.prototype.bind.apply(factory, args))();
} else {
return factory.apply(null, args);
}
}
function isConstructor(func) {
try {
new func();
Expand Down
4 changes: 2 additions & 2 deletions release-candidate/hilary.min.js

Large diffs are not rendered by default.

42 changes: 26 additions & 16 deletions release/hilary.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! hilary 2017-07-11 */
/*! hilary 2017-08-22 */

(function(register) {
"use strict";
Expand All @@ -9,6 +9,7 @@
INVALID_ARG: "InvalidArgument",
INVALID_REGISTRATION: "InvalidRegistration",
MODULE_NOT_FOUND: "ModuleNotFound",
MODULE_NOT_RESOLVED: "ModuleNotResolved",
BOOTSTRAP_FAILED: "BootstrapFailed"
},
container: {
Expand All @@ -25,6 +26,7 @@
RESOLVE_ARG: "resolve expects a moduleName (string) as the first argument, but instead saw this: ",
MODULE_NOT_FOUND: 'The module, "{{module}}", cannot be found',
MODULE_NOT_FOUND_RELYING: ', and is a dependency of, "{{startingModule}}"',
MODULE_THREW: 'The module, "{{module}}", cannot be resolved because it returned or threw an Error',
REGISTRATION_BLACK_LIST: "A module was registered with a reserved name: ",
PARENT_CONTAINER_ARG: "setParentScope expects the name of the parent scope, or an instance of Hilary"
},
Expand Down Expand Up @@ -573,7 +575,7 @@
if (!dependency) {
logger.trace("the following dependency was not resolved: " + item);
return cb(null, dependencies, relyingModuleName);
} else if (dependency.isException) {
} else if (dependency.isException || dependency instanceof Error) {
logger.error("the following dependency returned an exception: " + item);
return cb(dependency);
}
Expand All @@ -595,15 +597,15 @@
});
return next(err);
}
ctx.resolved = invoke(ctx.theModule.factory, dependencies);
ctx.resolved = invoke(ctx.theModule.name, ctx.theModule.factory, dependencies);
ctx.registerSingleton = ctx.theModule.singleton;
ctx.isResolved = true;
logger.trace("dependencies resolved for: " + ctx.name);
next(null, ctx);
});
} else if (is.function(ctx.theModule.factory) && ctx.theModule.factory.length === 0) {
logger.trace("the factory is a function and takes no arguments, returning the result of executing it: " + ctx.name);
ctx.resolved = invoke(ctx.theModule.factory);
ctx.resolved = invoke(ctx.theModule.name, ctx.theModule.factory);
} else {
logger.trace("the factory takes arguments and has no dependencies, returning the function as-is: " + ctx.name);
ctx.resolved = ctx.theModule.factory;
Expand Down Expand Up @@ -664,6 +666,26 @@
});
}
};
function invoke(name, factory, args) {
try {
if (isConstructor(factory)) {
if (args) {
args = [ null ].concat(args);
} else {
args = [ null ];
}
return new (Function.prototype.bind.apply(factory, args))();
} else {
return factory.apply(null, args);
}
} catch (e) {
return new Exception({
type: locale.errorTypes.MODULE_NOT_RESOLVED,
messages: [ locale.api.MODULE_THREW.replace(/{{module}}/, name), e.message ],
error: e
});
}
}
}
function parseDependencyName(dependencyName) {
var memberMatches = /\{([^}]+)\}/.exec(dependencyName), members = [];
Expand Down Expand Up @@ -704,18 +726,6 @@
return window[moduleName];
}
}
function invoke(factory, args) {
if (isConstructor(factory)) {
if (args) {
args = [ null ].concat(args);
} else {
args = [ null ];
}
return new (Function.prototype.bind.apply(factory, args))();
} else {
return factory.apply(null, args);
}
}
function isConstructor(func) {
try {
new func();
Expand Down
4 changes: 2 additions & 2 deletions release/hilary.min.js

Large diffs are not rendered by default.

43 changes: 26 additions & 17 deletions src/ResolveTasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
// short circuit
logger.trace('the following dependency was not resolved: ' + item);
return cb(null, dependencies, relyingModuleName);
} else if (dependency.isException) {
} else if (dependency.isException || dependency instanceof Error) {
// short circuit
logger.error('the following dependency returned an exception: ' + item);
return cb(dependency);
Expand All @@ -122,7 +122,7 @@
return next(err);
}

ctx.resolved = invoke(ctx.theModule.factory, dependencies);
ctx.resolved = invoke(ctx.theModule.name, ctx.theModule.factory, dependencies);
ctx.registerSingleton = ctx.theModule.singleton;
ctx.isResolved = true;

Expand All @@ -131,7 +131,7 @@
});
} else if (is.function(ctx.theModule.factory) && ctx.theModule.factory.length === 0) {
logger.trace('the factory is a function and takes no arguments, returning the result of executing it: ' + ctx.name);
ctx.resolved = invoke(ctx.theModule.factory);
ctx.resolved = invoke(ctx.theModule.name, ctx.theModule.factory);
} else {
// the module takes arguments and has no dependencies, this must be a factory
logger.trace('the factory takes arguments and has no dependencies, returning the function as-is: ' + ctx.name);
Expand Down Expand Up @@ -216,6 +216,29 @@
}

}; // /Ctor

function invoke (name, factory, args) {
try {
if (isConstructor(factory)) {
if (args) {
args = [null].concat(args);
} else {
args = [null];
}

return new (Function.prototype.bind.apply(factory, args))();
} else {
return factory.apply(null, args);
}
} catch (e) {
return new Exception({
type: locale.errorTypes.MODULE_NOT_RESOLVED,
messages: [locale.api.MODULE_THREW.replace(/{{module}}/, name), e.message],
error: e
});
}
}

} // /ResolveTasks

// Check to see if the dependency reduces members
Expand Down Expand Up @@ -271,20 +294,6 @@
}
}

function invoke (factory, args) {
if (isConstructor(factory)) {
if (args) {
args = [null].concat(args);
} else {
args = [null];
}

return new (Function.prototype.bind.apply(factory, args))();
} else {
return factory.apply(null, args);
}
}

function isConstructor (func) {
try {
new func();
Expand Down
2 changes: 2 additions & 0 deletions src/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
INVALID_ARG: 'InvalidArgument',
INVALID_REGISTRATION: 'InvalidRegistration',
MODULE_NOT_FOUND: 'ModuleNotFound',
MODULE_NOT_RESOLVED: 'ModuleNotResolved',
BOOTSTRAP_FAILED: 'BootstrapFailed'
},
container: {
Expand All @@ -24,6 +25,7 @@
RESOLVE_ARG: 'resolve expects a moduleName (string) as the first argument, but instead saw this: ',
MODULE_NOT_FOUND: 'The module, "{{module}}", cannot be found',
MODULE_NOT_FOUND_RELYING: ', and is a dependency of, "{{startingModule}}"',
MODULE_THREW: 'The module, "{{module}}", cannot be resolved because it returned or threw an Error',
// MODULE_NOT_RESOLVABLE: 'The module, "{{module}}", cannot be resolved because of a dependency exception',
// MODULE_NOT_RESOLVABLE_RELYING: ', causing a ripple effect for, "{{startingModule}}"',
REGISTRATION_BLACK_LIST: 'A module was registered with a reserved name: ',
Expand Down

0 comments on commit 3d0bc94

Please sign in to comment.