angular-cn/modules/angular2/src/di/exceptions.js

190 lines
5.2 KiB
JavaScript
Raw Normal View History

import {ListWrapper, List} from 'angular2/src/facade/collection';
import {stringify} from 'angular2/src/facade/lang';
function findFirstClosedCycle(keys:List) {
var res = [];
for(var i = 0; i < keys.length; ++i) {
if (ListWrapper.contains(res, keys[i])) {
ListWrapper.push(res, keys[i]);
return res;
} else {
ListWrapper.push(res, keys[i]);
}
}
return res;
}
2014-10-07 10:34:07 -04:00
function constructResolvingPath(keys:List) {
if (keys.length > 1) {
var reversed = findFirstClosedCycle(ListWrapper.reversed(keys));
var tokenStrs = ListWrapper.map(reversed, (k) => stringify(k.token));
return " (" + tokenStrs.join(' -> ') + ")";
} else {
return "";
}
}
2014-10-20 15:17:06 -04:00
2015-04-15 22:35:38 +00:00
/**
2015-04-17 03:29:05 -07:00
* Base class for all errors arising from misconfigured bindings.
2015-04-15 22:35:38 +00:00
*
* @exportedAs angular2/di_errors
*/
export class AbstractBindingError extends Error {
keys:List;
constructResolvingMessage:Function;
message;
// TODO(tbosch): Can't do key:Key as this results in a circular dependency!
constructor(key, constructResolvingMessage:Function) {
super();
this.keys = [key];
this.constructResolvingMessage = constructResolvingMessage;
this.message = this.constructResolvingMessage(this.keys);
}
// TODO(tbosch): Can't do key:Key as this results in a circular dependency!
addKey(key) {
ListWrapper.push(this.keys, key);
this.message = this.constructResolvingMessage(this.keys);
}
2014-10-07 09:21:00 -04:00
toString() {
return this.message;
}
}
2015-04-15 22:35:38 +00:00
/**
2015-04-17 03:29:05 -07:00
* Thrown when trying to retrieve a dependency by [Key] from [Injector], but the [Injector] does not have a [Binding]
* for [Key].
2015-04-15 22:35:38 +00:00
*
* @exportedAs angular2/di_errors
*/
export class NoBindingError extends AbstractBindingError {
// TODO(tbosch): Can't do key:Key as this results in a circular dependency!
constructor(key) {
2014-10-07 10:34:07 -04:00
super(key, function (keys:List) {
var first = stringify(ListWrapper.first(keys).token);
return `No provider for ${first}!${constructResolvingPath(keys)}`;
});
}
}
2015-04-15 22:35:38 +00:00
/**
2015-04-17 03:29:05 -07:00
* Thrown when trying to retrieve an async [Binding] using the sync API.
2015-04-15 22:35:38 +00:00
*
* ## Example
*
* ```javascript
* var injector = Injector.resolveAndCreate([
* bind(Number).toAsyncFactory(() => {
* return new Promise((resolve) => resolve(1 + 2));
* }),
* bind(String).toFactory((v) => { return "Value: " + v; }, [String])
* ]);
*
* injector.asyncGet(String).then((v) => expect(v).toBe('Value: 3'));
* expect(() => {
* injector.get(String);
* }).toThrowError(AsycBindingError);
* ```
*
2015-04-17 03:29:05 -07:00
* The above example throws because `String` depends on `Number` which is async. If any binding in the dependency
* graph is async then the graph can only be retrieved using the `asyncGet` API.
2015-04-15 22:35:38 +00:00
*
* @exportedAs angular2/di_errors
*/
export class AsyncBindingError extends AbstractBindingError {
// TODO(tbosch): Can't do key:Key as this results in a circular dependency!
constructor(key) {
2014-10-07 10:34:07 -04:00
super(key, function (keys:List) {
var first = stringify(ListWrapper.first(keys).token);
return `Cannot instantiate ${first} synchronously. ` +
2014-10-10 15:44:56 -04:00
`It is provided as a promise!${constructResolvingPath(keys)}`;
});
}
}
2015-04-15 22:35:38 +00:00
/**
2015-04-17 03:29:05 -07:00
* Thrown when dependencies form a cycle.
2015-04-15 22:35:38 +00:00
*
* ## Example:
*
* ```javascript
* class A {
* constructor(b:B) {}
* }
* class B {
* constructor(a:A) {}
* }
* ```
*
2015-04-17 03:29:05 -07:00
* Retrieving `A` or `B` throws a `CyclicDependencyError` as the graph above cannot be constructed.
2015-04-15 22:35:38 +00:00
*
* @exportedAs angular2/di_errors
*/
export class CyclicDependencyError extends AbstractBindingError {
// TODO(tbosch): Can't do key:Key as this results in a circular dependency!
constructor(key) {
2014-10-07 10:34:07 -04:00
super(key, function (keys:List) {
return `Cannot instantiate cyclic dependency!${constructResolvingPath(keys)}`;
});
}
}
2015-04-15 22:35:38 +00:00
/**
2015-04-17 03:29:05 -07:00
* Thrown when a constructing type returns with an Error.
2015-04-15 22:35:38 +00:00
*
2015-04-17 03:29:05 -07:00
* The `InstantiationError` class contains the original error plus the dependency graph which caused this object to be
2015-04-15 22:35:38 +00:00
* instantiated.
*
* @exportedAs angular2/di_errors
*/
export class InstantiationError extends AbstractBindingError {
// TODO(tbosch): Can't do key:Key as this results in a circular dependency!
constructor(originalException, key) {
2014-10-07 10:34:07 -04:00
super(key, function (keys:List) {
var first = stringify(ListWrapper.first(keys).token);
2014-10-07 10:34:07 -04:00
return `Error during instantiation of ${first}!${constructResolvingPath(keys)}.` +
` ORIGINAL ERROR: ${originalException}`;
});
}
}
2015-04-15 22:35:38 +00:00
/**
2015-04-17 03:29:05 -07:00
* Thrown when an object other then [Binding] (or [Type]) is passed to [Injector] creation.
2015-04-15 22:35:38 +00:00
*
* @exportedAs angular2/di_errors
*/
2014-10-07 09:21:00 -04:00
export class InvalidBindingError extends Error {
message:string;
2014-10-07 10:34:07 -04:00
constructor(binding) {
super();
this.message = `Invalid binding ${binding}`;
}
2014-10-07 09:21:00 -04:00
toString() {
return this.message;
}
}
2015-04-15 22:35:38 +00:00
/**
2015-04-17 03:29:05 -07:00
* Thrown when the class has no annotation information.
2015-04-15 22:35:38 +00:00
*
2015-04-17 03:29:05 -07:00
* Lack of annotation information prevents the [Injector] from determining which dependencies need to be injected into
* the constructor.
2015-04-15 22:35:38 +00:00
*
* @exportedAs angular2/di_errors
*/
2014-10-07 09:21:00 -04:00
export class NoAnnotationError extends Error {
message:string;
constructor(typeOrFunc) {
super();
this.message = `Cannot resolve all parameters for ${stringify(typeOrFunc)}.` +
` Make sure they all have valid type or annotations.`;
}
2014-10-07 09:21:00 -04:00
toString() {
return this.message;
}
2014-10-09 12:09:50 -04:00
}