docs(di): add docs to DI exceptions

This commit is contained in:
vsavkin 2015-09-17 13:13:30 -07:00
parent b2e9da4905
commit e1c0b1abcb

View File

@ -32,9 +32,16 @@ function constructResolvingPath(keys: any[]): string {
* Base class for all errors arising from misconfigured bindings. * Base class for all errors arising from misconfigured bindings.
*/ */
export class AbstractBindingError extends BaseException { export class AbstractBindingError extends BaseException {
/** @private */
message: string; message: string;
/** @private */
keys: Key[]; keys: Key[];
/** @private */
injectors: Injector[]; injectors: Injector[];
/** @private */
constructResolvingMessage: Function; constructResolvingMessage: Function;
constructor(injector: Injector, key: Key, constructResolvingMessage: Function) { constructor(injector: Injector, key: Key, constructResolvingMessage: Function) {
@ -57,6 +64,16 @@ export class AbstractBindingError extends BaseException {
/** /**
* Thrown when trying to retrieve a dependency by `Key` from {@link Injector}, but the * 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}. * {@link Injector} does not have a {@link Binding} for {@link Key}.
*
* ### Example ([live demo](http://plnkr.co/edit/vq8D3FRB9aGbnWJqtEPE?p=preview))
*
* ```typescript
* class A {
* constructor(b:B) {}
* }
*
* expect(() => Injector.resolveAndCreate([A])).toThrowError();
* ```
*/ */
export class NoBindingError extends AbstractBindingError { export class NoBindingError extends AbstractBindingError {
constructor(injector: Injector, key: Key) { constructor(injector: Injector, key: Key) {
@ -70,15 +87,15 @@ export class NoBindingError extends AbstractBindingError {
/** /**
* Thrown when dependencies form a cycle. * Thrown when dependencies form a cycle.
* *
* ## Example: * ### Example ([live demo](http://plnkr.co/edit/wYQdNos0Tzql3ei1EV9j?p=info))
* *
* ```javascript * ```typescript
* class A { * var injector = Injector.resolveAndCreate([
* constructor(b:B) {} * bind("one").toFactory((two) => "two", [[new Inject("two")]]),
* } * bind("two").toFactory((one) => "one", [[new Inject("one")]])
* class B { * ]);
* constructor(a:A) {} *
* } * expect(() => injector.get("one")).toThrowError();
* ``` * ```
* *
* Retrieving `A` or `B` throws a `CyclicDependencyError` as the graph above cannot be constructed. * Retrieving `A` or `B` throws a `CyclicDependencyError` as the graph above cannot be constructed.
@ -96,11 +113,35 @@ export class CyclicDependencyError extends AbstractBindingError {
* *
* The `InstantiationError` class contains the original error plus the dependency graph which caused * The `InstantiationError` class contains the original error plus the dependency graph which caused
* this object to be instantiated. * this object to be instantiated.
*
* ### Example ([live demo](http://plnkr.co/edit/7aWYdcqTQsP0eNqEdUAf?p=preview))
*
* ```typescript
* class A {
* constructor() {
* throw new Error('message');
* }
* }
*
* var injector = Injector.resolveAndCreate([A]);
* try {
* injector.get(A);
* } catch (e) {
* expect(e instanceof InstantiationError).toBe(true);
* expect(e.originalException.message).toEqual("message");
* expect(e.originalStack).toBeDefined();
* }
* ```
*/ */
export class InstantiationError extends WrappedException { export class InstantiationError extends WrappedException {
/** @private */
keys: Key[]; keys: Key[];
/** @private */
injectors: Injector[]; injectors: Injector[];
/** @private */
constructor(injector: Injector, originalException, originalStack, key: Key) { constructor(injector: Injector, originalException, originalStack, key: Key) {
super("DI Exception", originalException, originalStack, null); super("DI Exception", originalException, originalStack, null);
this.keys = [key]; this.keys = [key];
@ -125,6 +166,12 @@ export class InstantiationError extends WrappedException {
/** /**
* Thrown when an object other then {@link Binding} (or `Type`) is passed to {@link Injector} * Thrown when an object other then {@link Binding} (or `Type`) is passed to {@link Injector}
* creation. * creation.
*
* ### Example ([live demo](http://plnkr.co/edit/YatCFbPAMCL0JSSQ4mvH?p=preview))
*
* ```typescript
* expect(() => Injector.resolveAndCreate(["not a type"])).toThrowError();
* ```
*/ */
export class InvalidBindingError extends BaseException { export class InvalidBindingError extends BaseException {
constructor(binding) { constructor(binding) {
@ -138,6 +185,16 @@ export class InvalidBindingError extends BaseException {
* *
* Lack of annotation information prevents the {@link Injector} from determining which dependencies * Lack of annotation information prevents the {@link Injector} from determining which dependencies
* need to be injected into the constructor. * need to be injected into the constructor.
*
* ### Example ([live demo](http://plnkr.co/edit/rHnZtlNS7vJOPQ6pcVkm?p=preview))
*
* ```typescript
* class A {
* constructor(b) {}
* }
*
* expect(() => Injector.resolveAndCreate([A])).toThrowError();
* ```
*/ */
export class NoAnnotationError extends BaseException { export class NoAnnotationError extends BaseException {
constructor(typeOrFunc, params: any[][]) { constructor(typeOrFunc, params: any[][]) {
@ -161,13 +218,33 @@ export class NoAnnotationError extends BaseException {
/** /**
* Thrown when getting an object by index. * Thrown when getting an object by index.
*
* ### Example ([live demo](http://plnkr.co/edit/bRs0SX2OTQiJzqvjgl8P?p=preview))
*
* ```typescript
* class A {}
*
* var injector = Injector.resolveAndCreate([A]);
*
* expect(() => injector.getAt(100)).toThrowError();
* ```
*/ */
export class OutOfBoundsError extends BaseException { export class OutOfBoundsError extends BaseException {
constructor(index) { super(`Index ${index} is out-of-bounds.`); } constructor(index) { super(`Index ${index} is out-of-bounds.`); }
} }
// TODO: add a working example after alpha38 is released
/** /**
* Thrown when a multi binding and a regular binding are bound to the same token. * Thrown when a multi binding and a regular binding are bound to the same token.
*
* ### Example
*
* ```typescript
* expect(() => Injector.resolveAndCreate([
* new Binding("Strings", {toValue: "string1", multi: true}),
* new Binding("Strings", {toValue: "string2", multi: false})
* ])).toThrowError();
* ```
*/ */
export class MixingMultiBindingsWithRegularBindings extends BaseException { export class MixingMultiBindingsWithRegularBindings extends BaseException {
constructor(binding1, binding2) { constructor(binding1, binding2) {