Skip to content

Commit 8222fb0

Browse files
committed
fix(state): fail transition on exceptions in transition handler
closes angular-ui#2772
1 parent d2a0feb commit 8222fb0

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

src/state.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1170,7 +1170,7 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
11701170
$urlRouter.update(true);
11711171

11721172
return $state.current;
1173-
}, function (error) {
1173+
}).then(null, function (error) {
11741174
if ($state.transition !== transition) return TransitionSuperseded;
11751175

11761176
$state.transition = null;

test/stateSpec.js

+20
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ describe('state', function () {
120120
},
121121
controller: function() { log += "controller;"}
122122
})
123+
.state('onEnterFail', {
124+
onEnter: function() {
125+
throw new Error('negative onEnter');
126+
}
127+
})
123128
.state('badParam', {
124129
url: "/bad/{param:int}"
125130
})
@@ -551,6 +556,20 @@ describe('state', function () {
551556
'A.onEnter;');
552557
}));
553558

559+
it('sends $stateChangeError for exceptions in onEnter', inject(function ($state, $q, $rootScope) {
560+
var called;
561+
$rootScope.$on('$stateChangeError', function (ev, to, toParams, from, fromParams, options) {
562+
called = true;
563+
});
564+
565+
initStateTo(A);
566+
$state.transitionTo('onEnterFail');
567+
$q.flush();
568+
569+
expect(called).toBeTruthy();
570+
expect($state.current.name).toEqual(A.name);
571+
}));
572+
554573
it('doesn\'t transition to parent state when child has no URL', inject(function ($state, $q) {
555574
$state.transitionTo('about.sidebar'); $q.flush();
556575
expect($state.current.name).toEqual('about.sidebar');
@@ -1044,6 +1063,7 @@ describe('state', function () {
10441063
'logA',
10451064
'logA.logB',
10461065
'logA.logB.logC',
1066+
'onEnterFail',
10471067
'resolveFail',
10481068
'resolveTimeout',
10491069
'root',

0 commit comments

Comments
 (0)