Skip to content

Commit e1c0b1a

Browse files
committed
docs(di): add docs to DI exceptions
1 parent b2e9da4 commit e1c0b1a

File tree

1 file changed

+85
-8
lines changed

1 file changed

+85
-8
lines changed

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

Lines changed: 85 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,16 @@ function constructResolvingPath(keys: any[]): string {
3232
* Base class for all errors arising from misconfigured bindings.
3333
*/
3434
export class AbstractBindingError extends BaseException {
35+
/** @private */
3536
message: string;
37+
38+
/** @private */
3639
keys: Key[];
40+
41+
/** @private */
3742
injectors: Injector[];
43+
44+
/** @private */
3845
constructResolvingMessage: Function;
3946

4047
constructor(injector: Injector, key: Key, constructResolvingMessage: Function) {
@@ -57,6 +64,16 @@ export class AbstractBindingError extends BaseException {
5764
/**
5865
* Thrown when trying to retrieve a dependency by `Key` from {@link Injector}, but the
5966
* {@link Injector} does not have a {@link Binding} for {@link Key}.
67+
*
68+
* ### Example ([live demo](http://plnkr.co/edit/vq8D3FRB9aGbnWJqtEPE?p=preview))
69+
*
70+
* ```typescript
71+
* class A {
72+
* constructor(b:B) {}
73+
* }
74+
*
75+
* expect(() => Injector.resolveAndCreate([A])).toThrowError();
76+
* ```
6077
*/
6178
export class NoBindingError extends AbstractBindingError {
6279
constructor(injector: Injector, key: Key) {
@@ -70,15 +87,15 @@ export class NoBindingError extends AbstractBindingError {
7087
/**
7188
* Thrown when dependencies form a cycle.
7289
*
73-
* ## Example:
90+
* ### Example ([live demo](http://plnkr.co/edit/wYQdNos0Tzql3ei1EV9j?p=info))
7491
*
75-
* ```javascript
76-
* class A {
77-
* constructor(b:B) {}
78-
* }
79-
* class B {
80-
* constructor(a:A) {}
81-
* }
92+
* ```typescript
93+
* var injector = Injector.resolveAndCreate([
94+
* bind("one").toFactory((two) => "two", [[new Inject("two")]]),
95+
* bind("two").toFactory((one) => "one", [[new Inject("one")]])
96+
* ]);
97+
*
98+
* expect(() => injector.get("one")).toThrowError();
8299
* ```
83100
*
84101
* Retrieving `A` or `B` throws a `CyclicDependencyError` as the graph above cannot be constructed.
@@ -96,11 +113,35 @@ export class CyclicDependencyError extends AbstractBindingError {
96113
*
97114
* The `InstantiationError` class contains the original error plus the dependency graph which caused
98115
* this object to be instantiated.
116+
*
117+
* ### Example ([live demo](http://plnkr.co/edit/7aWYdcqTQsP0eNqEdUAf?p=preview))
118+
*
119+
* ```typescript
120+
* class A {
121+
* constructor() {
122+
* throw new Error('message');
123+
* }
124+
* }
125+
*
126+
* var injector = Injector.resolveAndCreate([A]);
127+
128+
* try {
129+
* injector.get(A);
130+
* } catch (e) {
131+
* expect(e instanceof InstantiationError).toBe(true);
132+
* expect(e.originalException.message).toEqual("message");
133+
* expect(e.originalStack).toBeDefined();
134+
* }
135+
* ```
99136
*/
100137
export class InstantiationError extends WrappedException {
138+
/** @private */
101139
keys: Key[];
140+
141+
/** @private */
102142
injectors: Injector[];
103143

144+
/** @private */
104145
constructor(injector: Injector, originalException, originalStack, key: Key) {
105146
super("DI Exception", originalException, originalStack, null);
106147
this.keys = [key];
@@ -125,6 +166,12 @@ export class InstantiationError extends WrappedException {
125166
/**
126167
* Thrown when an object other then {@link Binding} (or `Type`) is passed to {@link Injector}
127168
* creation.
169+
*
170+
* ### Example ([live demo](http://plnkr.co/edit/YatCFbPAMCL0JSSQ4mvH?p=preview))
171+
*
172+
* ```typescript
173+
* expect(() => Injector.resolveAndCreate(["not a type"])).toThrowError();
174+
* ```
128175
*/
129176
export class InvalidBindingError extends BaseException {
130177
constructor(binding) {
@@ -138,6 +185,16 @@ export class InvalidBindingError extends BaseException {
138185
*
139186
* Lack of annotation information prevents the {@link Injector} from determining which dependencies
140187
* need to be injected into the constructor.
188+
*
189+
* ### Example ([live demo](http://plnkr.co/edit/rHnZtlNS7vJOPQ6pcVkm?p=preview))
190+
*
191+
* ```typescript
192+
* class A {
193+
* constructor(b) {}
194+
* }
195+
*
196+
* expect(() => Injector.resolveAndCreate([A])).toThrowError();
197+
* ```
141198
*/
142199
export class NoAnnotationError extends BaseException {
143200
constructor(typeOrFunc, params: any[][]) {
@@ -161,13 +218,33 @@ export class NoAnnotationError extends BaseException {
161218

162219
/**
163220
* Thrown when getting an object by index.
221+
*
222+
* ### Example ([live demo](http://plnkr.co/edit/bRs0SX2OTQiJzqvjgl8P?p=preview))
223+
*
224+
* ```typescript
225+
* class A {}
226+
*
227+
* var injector = Injector.resolveAndCreate([A]);
228+
*
229+
* expect(() => injector.getAt(100)).toThrowError();
230+
* ```
164231
*/
165232
export class OutOfBoundsError extends BaseException {
166233
constructor(index) { super(`Index ${index} is out-of-bounds.`); }
167234
}
168235

236+
// TODO: add a working example after alpha38 is released
169237
/**
170238
* Thrown when a multi binding and a regular binding are bound to the same token.
239+
*
240+
* ### Example
241+
*
242+
* ```typescript
243+
* expect(() => Injector.resolveAndCreate([
244+
* new Binding("Strings", {toValue: "string1", multi: true}),
245+
* new Binding("Strings", {toValue: "string2", multi: false})
246+
* ])).toThrowError();
247+
* ```
171248
*/
172249
export class MixingMultiBindingsWithRegularBindings extends BaseException {
173250
constructor(binding1, binding2) {

0 commit comments

Comments
 (0)