Skip to content

fix(state): fail transition on exceptions in transition handler #2773

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 20 additions & 0 deletions test/stateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
})
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -1044,6 +1063,7 @@ describe('state', function () {
'logA',
'logA.logB',
'logA.logB.logC',
'onEnterFail',
'resolveFail',
'resolveTimeout',
'root',
Expand Down