feat(injector): Service look up failures include dependency path

This commit is contained in:
Misko Hevery 2011-10-25 21:28:48 -07:00
parent 48697a2b86
commit 03dd8c4f4c
2 changed files with 18 additions and 9 deletions

View file

@ -44,22 +44,25 @@ function createInjector(factories) {
});
return injector;
function injector(value){
if (!(value in instanceCache)) {
var factory = factories[value];
if (!factory) throw Error("Unknown provider for '" + value + "'.");
function injector(serviceId, path){
if (!(serviceId in instanceCache)) {
var factory = factories[serviceId];
path = path || [];
path.unshift(serviceId);
if (!factory) throw Error("Unknown provider for '" + path.join("' <- '") + "'.");
inferInjectionArgs(factory);
instanceCache[value] = invoke(null, factory);
instanceCache[serviceId] = invoke(null, factory, [], path);
path.shift();
}
return instanceCache[value];
return instanceCache[serviceId];
}
function invoke(self, fn, args){
function invoke(self, fn, args, path){
args = args || [];
var injectNames = fn.$inject || [];
var i = injectNames.length;
while(i--) {
args.unshift(injector(injectNames[i]));
args.unshift(injector(injectNames[i], path));
}
return fn.apply(self, args);
}

View file

@ -62,12 +62,18 @@ describe('injector', function() {
});
it('should provide usefull message if no provider', function() {
it('should provide useful message if no provider', function() {
expect(function() {
injector('idontexist');
}).toThrow("Unknown provider for 'idontexist'.");
});
it('should proved path to the missing provider', function(){
expect(function() {
injector('idontexist', ['a', 'b']);
}).toThrow("Unknown provider for 'idontexist' <- 'a' <- 'b'.");
});
it('should autostart eager services', function() {
var log = '';
providers('eager', function() {log += 'eager;'; return 'foo';}, {$eager: true});