feat(di): changed InstantiationError to print the original stack

This commit is contained in:
vsavkin 2015-06-27 20:01:05 -07:00
parent 56245c6aa2
commit eb0fd7967c
3 changed files with 15 additions and 16 deletions

View File

@ -36,8 +36,8 @@ export class AbstractBindingError extends BaseException {
keys: List<any>; keys: List<any>;
constructResolvingMessage: Function; constructResolvingMessage: Function;
// TODO(tbosch): Can't do key:Key as this results in a circular dependency! // TODO(tbosch): Can't do key:Key as this results in a circular dependency!
constructor(key, constructResolvingMessage: Function) { constructor(key, constructResolvingMessage: Function, originalException?, originalStack?) {
super(); super(null, originalException, originalStack);
this.keys = [key]; this.keys = [key];
this.constructResolvingMessage = constructResolvingMessage; this.constructResolvingMessage = constructResolvingMessage;
this.message = this.constructResolvingMessage(this.keys); this.message = this.constructResolvingMessage(this.keys);
@ -138,19 +138,18 @@ export class CyclicDependencyError extends AbstractBindingError {
* @exportedAs angular2/di_errors * @exportedAs angular2/di_errors
*/ */
export class InstantiationError extends AbstractBindingError { export class InstantiationError extends AbstractBindingError {
cause;
causeKey; causeKey;
stack;
// TODO(tbosch): Can't do key:Key as this results in a circular dependency! // TODO(tbosch): Can't do key:Key as this results in a circular dependency!
constructor(cause, stack, key) { constructor(originalException, originalStack, key) {
super(key, function(keys: List<any>) { super(key, function(keys: List<any>) {
var first = stringify(ListWrapper.first(keys).token); var first = stringify(ListWrapper.first(keys).token);
return `Error during instantiation of ${first}!${constructResolvingPath(keys)}. ORIGINAL ERROR: ${cause}`; return `Error during instantiation of ${first}!${constructResolvingPath(keys)}.` +
}); ` ORIGINAL ERROR: ${originalException}` +
this.cause = cause; `\n\n ORIGINAL STACK: ${originalStack}`;
}, originalException, originalStack);
this.causeKey = key; this.causeKey = key;
this.stack = stack;
} }
} }

View File

@ -55,8 +55,7 @@ main() {
}); });
describe('Error handling', () { describe('Error handling', () {
//TODO: vsavkin reenable this test after merging DI and EI it('should preserve Error stack traces thrown from components', inject([
xit('should preserve Error stack traces thrown from components', inject([
TestComponentBuilder, TestComponentBuilder,
AsyncTestCompleter AsyncTestCompleter
], (tb, async) { ], (tb, async) {
@ -65,13 +64,13 @@ main() {
directives: [ThrowingComponent])) directives: [ThrowingComponent]))
.createAsync(Dummy).catchError((e, stack) { .createAsync(Dummy).catchError((e, stack) {
expect(stack.toString().split('\n')[0]).toEqual(e.message); expect(e.message).toContain("MockException");
expect(e.message).toContain("functionThatThrows");
async.done(); async.done();
}); });
})); }));
//TODO: vsavkin reenable this test after merging DI and EI it('should preserve non-Error stack traces thrown from components', inject([
xit('should preserve non-Error stack traces thrown from components', inject([
TestComponentBuilder, TestComponentBuilder,
AsyncTestCompleter AsyncTestCompleter
], (tb, async) { ], (tb, async) {
@ -80,7 +79,8 @@ main() {
directives: [ThrowingComponent2])) directives: [ThrowingComponent2]))
.createAsync(Dummy).catchError((e, stack) { .createAsync(Dummy).catchError((e, stack) {
expect(stack.toString().split('\n')[0]).toEqual(e.message); expect(e.message).toContain("NonError");
expect(e.message).toContain("functionThatThrows");
async.done(); async.done();
}); });
})); }));

View File

@ -250,7 +250,7 @@ export function main() {
} catch (e) { } catch (e) {
expect(e.message) expect(e.message)
.toContain(`Error during instantiation of Engine! (${stringify(Car)} -> Engine)`); .toContain(`Error during instantiation of Engine! (${stringify(Car)} -> Engine)`);
expect(e.cause instanceof BaseException).toBeTruthy(); expect(e.originalException instanceof BaseException).toBeTruthy();
expect(e.causeKey.token).toEqual(Engine); expect(e.causeKey.token).toEqual(Engine);
} }
}); });