fix(di): improve error messages for invalid bindings

Fixes #1515

Closes #1573
This commit is contained in:
Pawel Kozlowski 2015-04-28 14:24:20 +02:00 committed by Misko Hevery
parent c0f3778dda
commit ee1b574baf
3 changed files with 7 additions and 4 deletions

View File

@ -163,7 +163,7 @@ export class InvalidBindingError extends Error {
message:string;
constructor(binding) {
super();
this.message = `Invalid binding ${binding}`;
this.message = `Invalid binding - only instances of Binding and Type are allowed, got: ${binding}`;
}
toString():string {

View File

@ -379,7 +379,7 @@ function _resolveBindings(bindings:List): List {
} else if (unresolved instanceof List) {
resolved = _resolveBindings(unresolved);
} else if (unresolved instanceof BindingBuilder) {
throw new InvalidBindingError(unresolved.token);
throw new InvalidBindingError('BindingBuilder with ' + unresolved.token + ' token');
} else {
throw new InvalidBindingError(unresolved);
}

View File

@ -205,8 +205,11 @@ export function main() {
});
it('should throw when given invalid bindings', function () {
expect(() => Injector.resolveAndCreate(["blah"])).toThrowError('Invalid binding blah');
expect(() => Injector.resolveAndCreate([bind("blah")])).toThrowError('Invalid binding blah');
expect(() => Injector.resolveAndCreate(["blah"]))
.toThrowError('Invalid binding - only instances of Binding and Type are allowed, got: blah');
expect(() => Injector.resolveAndCreate([bind("blah")]))
.toThrowError('Invalid binding - only instances of Binding and Type are allowed, ' +
'got: BindingBuilder with blah token');
});
it('should provide itself', function () {