Permalink
Browse files

fix(lefecycle): handle thrown errors

reject the promise chain with the error thrown by the invoked lifecycle method
  • Loading branch information...
StrahilKazlachev committed Nov 6, 2016
1 parent 6d0c7b2 commit e7f12843fe0426389f2c921e9cbbc827d68f2a48
Showing with 13 additions and 2 deletions.
  1. +3 −1 src/lifecycle.js
  2. +10 −1 test/unit/lifecycle.spec.js
View
@@ -8,7 +8,9 @@
*/
export function invokeLifecycle(instance: any, name: string, model: any) {
if (typeof instance[name] === 'function') {
- return Promise.resolve(instance[name](model)).then(function(result) {
+ return Promise.resolve().then(() => {
+ return instance[name](model);
+ }).then(function (result) {
if (result !== null && result !== undefined) {
return result;
}
@@ -1,6 +1,6 @@
import {invokeLifecycle} from '../../src/lifecycle';
-describe('the lifecycle', function () {
+describe('"invokeLifecycle()"', function () {
const CAN_ACTIVATE = 'canActivate';
const ACTIVATE = 'activate';
const CAN_DEACTIVATE = 'canDeactivate';
@@ -136,4 +136,13 @@ describe('the lifecycle', function () {
});
});
});
+
+ it('propagates errors when the invocation of the specified lifecycle method throws', function (done) {
+ const expectedError = new Error();
+ const erroneousVM = { [CAN_ACTIVATE]() { throw expectedError; } };
+ invokeLifecycle(erroneousVM, CAN_ACTIVATE).catch((e) => e).then((e) => {
+ expect(e).toBe(expectedError);
+ done();
+ });
+ });
});

0 comments on commit e7f1284

Please sign in to comment.