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

103 lines
2.7 KiB
JavaScript
Raw Normal View History

import {ListWrapper, List} from 'facade/collection';
import {stringify} from 'facade/lang';
import {Key} from './key';
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
export class KeyMetadataError extends Error {}
2014-10-07 09:21:00 -04:00
export class ProviderError extends Error {
2014-10-07 10:34:07 -04:00
constructor(key:Key, constructResolvingMessage:Function) {
this.keys = [key];
this.constructResolvingMessage = constructResolvingMessage;
this.message = this.constructResolvingMessage(this.keys);
}
2014-10-07 10:34:07 -04:00
addKey(key:Key) {
ListWrapper.push(this.keys, key);
this.message = this.constructResolvingMessage(this.keys);
}
2014-10-07 09:21:00 -04:00
toString() {
return this.message;
}
}
export class NoProviderError extends ProviderError {
2014-10-07 10:34:07 -04:00
constructor(key:Key) {
super(key, function (keys:List) {
var first = stringify(ListWrapper.first(keys).token);
return `No provider for ${first}!${constructResolvingPath(keys)}`;
});
}
}
export class AsyncBindingError extends ProviderError {
2014-10-07 10:34:07 -04:00
constructor(key:Key) {
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)}`;
});
}
}
export class CyclicDependencyError extends ProviderError {
2014-10-07 10:34:07 -04:00
constructor(key:Key) {
super(key, function (keys:List) {
return `Cannot instantiate cyclic dependency!${constructResolvingPath(keys)}`;
});
}
}
export class InstantiationError extends ProviderError {
2014-10-07 10:34:07 -04:00
constructor(originalException, key:Key) {
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}`;
});
}
}
2014-10-07 09:21:00 -04:00
export class InvalidBindingError extends Error {
2014-10-07 10:34:07 -04:00
constructor(binding) {
this.message = `Invalid binding ${binding}`;
}
2014-10-07 09:21:00 -04:00
toString() {
return this.message;
}
}
2014-10-07 09:21:00 -04:00
export class NoAnnotationError extends Error {
constructor(typeOrFunc) {
this.message = `Cannot resolve all parameters for ${stringify(typeOrFunc)}`;
}
2014-10-07 09:21:00 -04:00
toString() {
return this.message;
}
2014-10-09 12:09:50 -04:00
}