From 8222fb0e7fd5eaaf6382f36db9ee9077a7bdbc6d Mon Sep 17 00:00:00 2001 From: Matt Ginzton Date: Fri, 27 May 2016 02:35:40 +0100 Subject: [PATCH] fix(state): fail transition on exceptions in transition handler closes #2772 --- src/state.js | 2 +- test/stateSpec.js | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/state.js b/src/state.js index cd56ebc4c..e3b455336 100644 --- a/src/state.js +++ b/src/state.js @@ -1170,7 +1170,7 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) { $urlRouter.update(true); return $state.current; - }, function (error) { + }).then(null, function (error) { if ($state.transition !== transition) return TransitionSuperseded; $state.transition = null; diff --git a/test/stateSpec.js b/test/stateSpec.js index 24553bae3..7de33398d 100644 --- a/test/stateSpec.js +++ b/test/stateSpec.js @@ -120,6 +120,11 @@ describe('state', function () { }, controller: function() { log += "controller;"} }) + .state('onEnterFail', { + onEnter: function() { + throw new Error('negative onEnter'); + } + }) .state('badParam', { url: "/bad/{param:int}" }) @@ -551,6 +556,20 @@ describe('state', function () { 'A.onEnter;'); })); + it('sends $stateChangeError for exceptions in onEnter', inject(function ($state, $q, $rootScope) { + var called; + $rootScope.$on('$stateChangeError', function (ev, to, toParams, from, fromParams, options) { + called = true; + }); + + initStateTo(A); + $state.transitionTo('onEnterFail'); + $q.flush(); + + expect(called).toBeTruthy(); + expect($state.current.name).toEqual(A.name); + })); + it('doesn\'t transition to parent state when child has no URL', inject(function ($state, $q) { $state.transitionTo('about.sidebar'); $q.flush(); expect($state.current.name).toEqual('about.sidebar'); @@ -1044,6 +1063,7 @@ describe('state', function () { 'logA', 'logA.logB', 'logA.logB.logC', + 'onEnterFail', 'resolveFail', 'resolveTimeout', 'root',