Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 03dd8c4

Browse files
committed
feat(injector): Service look up failures include dependency path
1 parent 48697a2 commit 03dd8c4

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

src/Injector.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,25 @@ function createInjector(factories) {
4444
});
4545
return injector;
4646

47-
function injector(value){
48-
if (!(value in instanceCache)) {
49-
var factory = factories[value];
50-
if (!factory) throw Error("Unknown provider for '" + value + "'.");
47+
function injector(serviceId, path){
48+
if (!(serviceId in instanceCache)) {
49+
var factory = factories[serviceId];
50+
path = path || [];
51+
path.unshift(serviceId);
52+
if (!factory) throw Error("Unknown provider for '" + path.join("' <- '") + "'.");
5153
inferInjectionArgs(factory);
52-
instanceCache[value] = invoke(null, factory);
54+
instanceCache[serviceId] = invoke(null, factory, [], path);
55+
path.shift();
5356
}
54-
return instanceCache[value];
57+
return instanceCache[serviceId];
5558
}
5659

57-
function invoke(self, fn, args){
60+
function invoke(self, fn, args, path){
5861
args = args || [];
5962
var injectNames = fn.$inject || [];
6063
var i = injectNames.length;
6164
while(i--) {
62-
args.unshift(injector(injectNames[i]));
65+
args.unshift(injector(injectNames[i], path));
6366
}
6467
return fn.apply(self, args);
6568
}

test/InjectorSpec.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,18 @@ describe('injector', function() {
6262
});
6363

6464

65-
it('should provide usefull message if no provider', function() {
65+
it('should provide useful message if no provider', function() {
6666
expect(function() {
6767
injector('idontexist');
6868
}).toThrow("Unknown provider for 'idontexist'.");
6969
});
7070

71+
it('should proved path to the missing provider', function(){
72+
expect(function() {
73+
injector('idontexist', ['a', 'b']);
74+
}).toThrow("Unknown provider for 'idontexist' <- 'a' <- 'b'.");
75+
});
76+
7177
it('should autostart eager services', function() {
7278
var log = '';
7379
providers('eager', function() {log += 'eager;'; return 'foo';}, {$eager: true});

0 commit comments

Comments
 (0)