forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexceptions.ts
More file actions
183 lines (163 loc) · 5.29 KB
/
Copy pathexceptions.ts
File metadata and controls
183 lines (163 loc) · 5.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import {ListWrapper} from 'angular2/src/core/facade/collection';
import {stringify, BaseException, isBlank} from 'angular2/src/core/facade/lang';
import {Key} from './key';
import {Injector} from './injector';
function findFirstClosedCycle(keys: any[]): any[] {
var res = [];
for (var i = 0; i < keys.length; ++i) {
if (ListWrapper.contains(res, keys[i])) {
res.push(keys[i]);
return res;
} else {
res.push(keys[i]);
}
}
return res;
}
function constructResolvingPath(keys: any[]): string {
if (keys.length > 1) {
var reversed = findFirstClosedCycle(ListWrapper.reversed(keys));
var tokenStrs = ListWrapper.map(reversed, (k) => stringify(k.token));
return " (" + tokenStrs.join(' -> ') + ")";
} else {
return "";
}
}
/**
* Base class for all errors arising from misconfigured bindings.
*/
export class AbstractBindingError extends BaseException {
name: string;
message: string;
keys: Key[];
injectors: Injector[];
constructResolvingMessage: Function;
constructor(injector: Injector, key: Key, constructResolvingMessage: Function, originalException?,
originalStack?) {
super("DI Exception", originalException, originalStack, null);
this.keys = [key];
this.injectors = [injector];
this.constructResolvingMessage = constructResolvingMessage;
this.message = this.constructResolvingMessage(this.keys);
}
addKey(injector: Injector, key: Key): void {
this.injectors.push(injector);
this.keys.push(key);
this.message = this.constructResolvingMessage(this.keys);
}
get context() { return this.injectors[this.injectors.length - 1].debugContext(); }
toString(): string { return this.message; }
}
/**
* Thrown when trying to retrieve a dependency by `Key` from {@link Injector}, but the
* {@link Injector} does not have a {@link Binding} for {@link Key}.
*/
export class NoBindingError extends AbstractBindingError {
constructor(injector: Injector, key: Key) {
super(injector, key, function(keys: any[]) {
var first = stringify(ListWrapper.first(keys).token);
return `No provider for ${first}!${constructResolvingPath(keys)}`;
});
}
}
/**
* Thrown when dependencies form a cycle.
*
* ## Example:
*
* ```javascript
* class A {
* constructor(b:B) {}
* }
* class B {
* constructor(a:A) {}
* }
* ```
*
* Retrieving `A` or `B` throws a `CyclicDependencyError` as the graph above cannot be constructed.
*/
export class CyclicDependencyError extends AbstractBindingError {
constructor(injector: Injector, key: Key) {
super(injector, key, function(keys: any[]) {
return `Cannot instantiate cyclic dependency!${constructResolvingPath(keys)}`;
});
}
}
/**
* Thrown when a constructing type returns with an Error.
*
* The `InstantiationError` class contains the original error plus the dependency graph which caused
* this object to be instantiated.
*/
export class InstantiationError extends AbstractBindingError {
causeKey: Key;
constructor(injector: Injector, originalException, originalStack, key: Key) {
super(injector, key, function(keys: any[]) {
var first = stringify(ListWrapper.first(keys).token);
return `Error during instantiation of ${first}!${constructResolvingPath(keys)}.`;
}, originalException, originalStack);
this.causeKey = key;
}
}
/**
* Thrown when an object other then {@link Binding} (or `Type`) is passed to {@link Injector}
* creation.
*/
export class InvalidBindingError extends BaseException {
message: string;
constructor(binding) {
super();
this.message = "Invalid binding - only instances of Binding and Type are allowed, got: " +
binding.toString();
}
toString(): string { return this.message; }
}
/**
* Thrown when the class has no annotation information.
*
* Lack of annotation information prevents the {@link Injector} from determining which dependencies
* need to be injected into the constructor.
*/
export class NoAnnotationError extends BaseException {
name: string;
message: string;
constructor(typeOrFunc, params: any[][]) {
super();
var signature = [];
for (var i = 0, ii = params.length; i < ii; i++) {
var parameter = params[i];
if (isBlank(parameter) || parameter.length == 0) {
signature.push('?');
} else {
signature.push(ListWrapper.map(parameter, stringify).join(' '));
}
}
this.message = "Cannot resolve all parameters for " + stringify(typeOrFunc) + "(" +
signature.join(', ') + "). " +
'Make sure they all have valid type or annotations.';
}
toString(): string { return this.message; }
}
/**
* Thrown when getting an object by index.
*/
export class OutOfBoundsError extends BaseException {
message: string;
constructor(index) {
super();
this.message = `Index ${index} is out-of-bounds.`;
}
toString(): string { return this.message; }
}
/**
* Thrown when a multi binding and a regular binding are bound to the same token.
*/
export class MixingMultiBindingsWithRegularBindings extends BaseException {
message: string;
constructor(binding1, binding2) {
super();
this.message = "Cannot mix multi bindings and regular bindings, got: " + binding1.toString() +
" " + binding2.toString();
}
toString(): string { return this.message; }
}