From e23004df52421a18628f1f45774781bd171f12a2 Mon Sep 17 00:00:00 2001 From: Pawel Kozlowski Date: Mon, 20 Apr 2015 15:35:16 +0200 Subject: [PATCH] fix(di): capture original exception in InvalidBindingError Fixes #1406 Closes #1459 --- modules/angular2/src/di/exceptions.js | 8 ++++++-- modules/angular2/test/di/injector_spec.js | 6 ++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/modules/angular2/src/di/exceptions.js b/modules/angular2/src/di/exceptions.js index ffc9da8234..ed0c59838c 100644 --- a/modules/angular2/src/di/exceptions.js +++ b/modules/angular2/src/di/exceptions.js @@ -140,13 +140,17 @@ export class CyclicDependencyError extends AbstractBindingError { * @exportedAs angular2/di_errors */ export class InstantiationError extends AbstractBindingError { + cause; + causeKey; // TODO(tbosch): Can't do key:Key as this results in a circular dependency! - constructor(originalException, key) { + constructor(cause, key) { super(key, function (keys:List) { var first = stringify(ListWrapper.first(keys).token); return `Error during instantiation of ${first}!${constructResolvingPath(keys)}.` + - ` ORIGINAL ERROR: ${originalException}`; + ` ORIGINAL ERROR: ${cause}`; }); + this.cause = cause; + this.causeKey = key; } } diff --git a/modules/angular2/test/di/injector_spec.js b/modules/angular2/test/di/injector_spec.js index b31e72a171..8bd11f2a1e 100644 --- a/modules/angular2/test/di/injector_spec.js +++ b/modules/angular2/test/di/injector_spec.js @@ -1,4 +1,4 @@ -import {isBlank} from 'angular2/src/facade/lang'; +import {isBlank, BaseException} from 'angular2/src/facade/lang'; import {describe, ddescribe, it, iit, expect, beforeEach} from 'angular2/test_lib'; import {Injector, Inject, InjectLazy, Optional, bind, ResolvedBinding} from 'angular2/di'; @@ -7,7 +7,7 @@ class Engine { class BrokenEngine { constructor() { - throw "Broken Engine"; + throw new BaseException("Broken Engine"); } } @@ -251,6 +251,8 @@ export function main() { throw "Must throw"; } catch (e) { expect(e.message).toContain("Error during instantiation of Engine! (Car -> Engine)"); + expect(e.cause instanceof BaseException).toBeTruthy(); + expect(e.causeKey.token).toEqual(Engine); } });