Skip to content

Commit 2ccc65d

Browse files
committed
fix: Improve error message on missing dependency
1 parent 7501ad1 commit 2ccc65d

16 files changed

+85
-48
lines changed

modules/angular2/src/core/annotations_impl/di.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {CONST} from 'angular2/src/facade/lang';
1+
import {CONST, stringify} from 'angular2/src/facade/lang';
22
import {DependencyAnnotation} from 'angular2/src/di/annotations_impl';
33

44
/**
@@ -41,6 +41,7 @@ export class Attribute extends DependencyAnnotation {
4141
// account.
4242
return this;
4343
}
44+
toString() { return `@Attribute(${stringify(this.attributeName)})`; }
4445
}
4546

4647
/**
@@ -53,4 +54,5 @@ export class Attribute extends DependencyAnnotation {
5354
@CONST()
5455
export class Query extends DependencyAnnotation {
5556
constructor(public directive: any) { super(); }
57+
toString() { return `@Query(${stringify(this.directive)})`; }
5658
}

modules/angular2/src/core/annotations_impl/visibility.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ export class Visibility extends DependencyAnnotation {
99
}
1010

1111
get includeSelf(): boolean { return isBlank(this._includeSelf) ? false : this._includeSelf; }
12+
toString() {
13+
return `@Visibility(depth: ${this.depth}, crossComponentBoundaries: ${this.crossComponentBoundaries}, includeSelf: ${this.includeSelf}})`;
14+
}
1215
}
1316

1417
/**
@@ -51,6 +54,7 @@ export class Visibility extends DependencyAnnotation {
5154
@CONST()
5255
export class Self extends Visibility {
5356
constructor() { super(0, false, true); }
57+
toString() { return `@Self()`; }
5458
}
5559

5660
// make constants after switching to ts2dart
@@ -102,6 +106,7 @@ export var self = new Self();
102106
@CONST()
103107
export class Parent extends Visibility {
104108
constructor({self}: {self?: boolean} = {}) { super(1, false, self); }
109+
toString() { return `@Parent(self: ${this.includeSelf}})`; }
105110
}
106111

107112
/**
@@ -164,6 +169,7 @@ export class Parent extends Visibility {
164169
@CONST()
165170
export class Ancestor extends Visibility {
166171
constructor({self}: {self?: boolean} = {}) { super(999999, false, self); }
172+
toString() { return `@Ancestor(self: ${this.includeSelf}})`; }
167173
}
168174

169175
/**
@@ -203,4 +209,5 @@ export class Ancestor extends Visibility {
203209
@CONST()
204210
export class Unbounded extends Visibility {
205211
constructor({self}: {self?: boolean} = {}) { super(999999, true, self); }
212+
toString() { return `@Unbounded(self: ${this.includeSelf}})`; }
206213
}

modules/angular2/src/di/annotations_impl.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {CONST} from "angular2/src/facade/lang";
1+
import {CONST, stringify} from "angular2/src/facade/lang";
22

33
/**
44
* A parameter annotation that specifies a dependency.
@@ -15,6 +15,7 @@ import {CONST} from "angular2/src/facade/lang";
1515
@CONST()
1616
export class Inject {
1717
constructor(public token) {}
18+
toString() { return `@Inject(${stringify(this.token)})`; }
1819
}
1920

2021
/**
@@ -33,6 +34,7 @@ export class Inject {
3334
@CONST()
3435
export class InjectPromise {
3536
constructor(public token) {}
37+
toString() { return `@InjectPromise(${stringify(this.token)})`; }
3638
}
3739

3840
/**
@@ -51,6 +53,7 @@ export class InjectPromise {
5153
@CONST()
5254
export class InjectLazy {
5355
constructor(public token) {}
56+
toString() { return `@InjectLazy(${stringify(this.token)})`; }
5457
}
5558

5659
/**
@@ -69,6 +72,7 @@ export class InjectLazy {
6972
*/
7073
@CONST()
7174
export class Optional {
75+
toString() { return `@Optional()`; }
7276
}
7377

7478
/**

modules/angular2/src/di/binding.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -437,22 +437,27 @@ export class BindingBuilder {
437437
}
438438
}
439439

440-
function _constructDependencies(factoryFunction: Function, dependencies: List<any>) {
441-
return isBlank(dependencies) ?
442-
_dependenciesFor(factoryFunction) :
443-
ListWrapper.map(dependencies, (t) => _extractToken(factoryFunction, t));
440+
function _constructDependencies(factoryFunction: Function,
441+
dependencies: List<any>): List<Dependency> {
442+
if (isBlank(dependencies)) {
443+
return _dependenciesFor(factoryFunction);
444+
} else {
445+
var params: List<List<any>> = ListWrapper.map(dependencies, (t) => [t]);
446+
return ListWrapper.map(dependencies, (t) => _extractToken(factoryFunction, t, params));
447+
}
444448
}
445449

446-
function _dependenciesFor(typeOrFunc): List<any> {
450+
function _dependenciesFor(typeOrFunc): List<Dependency> {
447451
var params = reflector.parameters(typeOrFunc);
448452
if (isBlank(params)) return [];
449453
if (ListWrapper.any(params, (p) => isBlank(p))) {
450-
throw new NoAnnotationError(typeOrFunc);
454+
throw new NoAnnotationError(typeOrFunc, params);
451455
}
452-
return ListWrapper.map(params, (p) => _extractToken(typeOrFunc, p));
456+
return ListWrapper.map(params, (p: List<any>) => _extractToken(typeOrFunc, p, params));
453457
}
454458

455-
function _extractToken(typeOrFunc, annotations) {
459+
function _extractToken(typeOrFunc, annotations /*List<any> | any*/,
460+
params: List<List<any>>): Dependency {
456461
var depProps = [];
457462
var token = null;
458463
var optional = false;
@@ -496,7 +501,7 @@ function _extractToken(typeOrFunc, annotations) {
496501
if (isPresent(token)) {
497502
return _createDependency(token, asPromise, lazy, optional, depProps);
498503
} else {
499-
throw new NoAnnotationError(typeOrFunc);
504+
throw new NoAnnotationError(typeOrFunc, params);
500505
}
501506
}
502507

modules/angular2/src/di/exceptions.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {ListWrapper, List} from 'angular2/src/facade/collection';
2-
import {stringify, BaseException} from 'angular2/src/facade/lang';
2+
import {stringify, BaseException, isBlank} from 'angular2/src/facade/lang';
33

44
function findFirstClosedCycle(keys: List<any>): List<any> {
55
var res = [];
@@ -179,9 +179,19 @@ export class InvalidBindingError extends BaseException {
179179
export class NoAnnotationError extends BaseException {
180180
name: string;
181181
message: string;
182-
constructor(typeOrFunc) {
182+
constructor(typeOrFunc, params: List<List<any>>) {
183183
super();
184-
this.message = "Cannot resolve all parameters for " + stringify(typeOrFunc) + ". " +
184+
var signature = ListWrapper.create();
185+
for (var i = 0, ii = params.length; i < ii; i++) {
186+
var parameter = params[i];
187+
if (isBlank(parameter) || parameter.length == 0) {
188+
ListWrapper.push(signature, '?');
189+
} else {
190+
ListWrapper.push(signature, ListWrapper.map(parameter, stringify).join(' '));
191+
}
192+
}
193+
this.message = "Cannot resolve all parameters for " + stringify(typeOrFunc) + "(" +
194+
signature.join(', ') + "). " +
185195
'Make sure they all have valid type or annotations.';
186196
}
187197

modules/angular2/src/di/forward_ref.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Type} from 'angular2/src/facade/lang';
1+
import {Type, stringify} from 'angular2/src/facade/lang';
22

33
export interface ForwardRefFn { (): any; }
44

@@ -30,6 +30,7 @@ export interface ForwardRefFn { (): any; }
3030
*/
3131
export function forwardRef(forwardRefFn: ForwardRefFn): Type {
3232
(<any>forwardRefFn).__forward_ref__ = forwardRef;
33+
(<any>forwardRefFn).toString = function() { return stringify(this()); };
3334
return (<Type><any>forwardRefFn);
3435
}
3536

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
library angular2.di.type_info;
2+
3+
// In dart always return empty, as we can get the co
4+
argsLength(Type type) => 0;

modules/angular2/src/di/type_info.ts

Whitespace-only changes.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {Type} from 'angular2/src/facade/lang';
2+
import {GetterFn, SetterFn, MethodFn} from './types';
3+
import {List} from 'angular2/src/facade/collection';
4+
5+
export interface PlatformReflectionCapabilities {
6+
factory(type: Type): Function;
7+
interfaces(type: Type): List<any>;
8+
parameters(type: Type): List<List<any>>;
9+
annotations(type: Type): List<any>;
10+
getter(name: string): GetterFn;
11+
setter(name: string): SetterFn;
12+
method(name: string): MethodFn;
13+
}

modules/angular2/src/reflection/reflection.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,18 @@ library reflection.reflection;
33
import 'reflector.dart';
44
import 'types.dart';
55
export 'reflector.dart';
6+
import 'platform_reflection_capabilities.dart';
67
import 'package:angular2/src/facade/lang.dart';
78

8-
class NoReflectionCapabilities implements IReflectionCapabilities {
9+
class NoReflectionCapabilities implements PlatformReflectionCapabilities {
910
Function factory(Type type) {
1011
throw "Cannot find reflection information on ${stringify(type)}";
1112
}
1213

14+
List interfaces(Type type) {
15+
throw "Cannot find reflection information on ${stringify(type)}";
16+
}
17+
1318
List parameters(Type type) {
1419
throw "Cannot find reflection information on ${stringify(type)}";
1520
}

0 commit comments

Comments
 (0)