feat(injector): change injector to show the full path when error happens in a constructor (async)

This commit is contained in:
vsavkin 2014-10-07 09:04:11 -04:00
parent e7666d0612
commit 62004e22e0
4 changed files with 44 additions and 4 deletions

View File

@ -186,7 +186,9 @@ class _AsyncInjectorStrategy {
this.injector._setInstance(key, _constructing);
var deps = this._resolveDependencies(key, binding);
var future = FutureWrapper.wait(deps).
var depsFuture = FutureWrapper.wait(deps);
var future = FutureWrapper.catchError(depsFuture, (e) => this._errorHandler(key, e)).
then(deps => this._findOrCreate(key, binding, deps)).
then(instance => this._cacheInstance(key, instance));
@ -205,10 +207,19 @@ class _AsyncInjectorStrategy {
}
}
_errorHandler(key:Key, e):Future {
if (e instanceof ProviderError) e.addKey(key);
return FutureWrapper.error(e);
}
_findOrCreate(key:Key, binding: Binding, deps:List) {
var instance = this.injector._getInstance(key);
if (! _isWaiting(instance)) return instance;
return binding.factory(deps);
try {
var instance = this.injector._getInstance(key);
if (!_isWaiting(instance)) return instance;
return binding.factory(deps);
} catch (e) {
throw new InstantiationError(e, key);
}
}
_cacheInstance(key, instance) {

View File

@ -100,6 +100,19 @@ export function main () {
done();
});
});
it('should show the full path when error happens in a constructor', function(done) {
var injector = new Injector([
UserController,
bind(UserList).toAsyncFactory([], function(){throw "Broken UserList";})
]);
var future = injector.asyncGet(UserController);
FutureWrapper.catchError(future, function (e) {
expect(e.message).toContain("Error during instantiation of UserList! (UserController -> UserList)");
done();
});
});
});
describe("get", function () {

View File

@ -8,7 +8,15 @@ class FutureWrapper {
return new Future.value(obj);
}
static Future error(obj) {
return new Future.error(obj);
}
static Future wait(List<Future> futures){
return Future.wait(futures);
}
static Future catchError(Future future, Function onError){
return future.catchError(onError);
}
}

View File

@ -5,8 +5,16 @@ export class FutureWrapper {
return Future.resolve(obj);
}
static error(obj):Future {
return Future.reject(obj);
}
static wait(futures):Future {
if (futures.length == 0) return Future.resolve([]);
return Future.all(futures);
}
static catchError(future:Future, onError:Function):Future {
return future.catch(onError);
}
}