Skip to content

feat($state) Detect looping TransitionSuperceded (application state redirects) #1210

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

Closed
wants to merge 1 commit into from
Closed
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
54 changes: 52 additions & 2 deletions src/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
$StateProvider.$inject = ['$urlRouterProvider', '$urlMatcherFactoryProvider'];
function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {

var root, states = {}, $state, queue = {}, abstractKey = 'abstract';
var root, states = {}, $state, queue = {}, abstractKey = 'abstract', maximumRedirects = 10;

// Builds state properties from definition passed to registerState()
var stateBuilder = {
Expand Down Expand Up @@ -509,6 +509,40 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
return this;
}

/**
* @ngdoc function
* @name ui.router.state.$stateProvider#maxRedirects
* @methodOf ui.router.state.$stateProvider
*
* @description
* A method that sets the maximum number of nested Transition Superceded (application redirects) before UI-Router
* throws an Error.
*
* @example
* Set max redirects:
* <pre>
* angular.module('app', ['ui.router']);
* app.config(function ($stateProvider) {
* var newMax = $stateProvider.maxRedirects(30);
* });
* </pre>
*
* Get max redirects
* <pre>
* angular.module('app', ['ui.router']);
* app.config(function ($stateProvider) {
* var max = $stateProvider.maxRedirects();
* });
* </pre>
*/
this.maxRedirects = maxRedirects;
function maxRedirects(newMaximum) {
if (isDefined(newMaximum)) {
maximumRedirects = newMaximum;
}
return maximumRedirects;
}

/**
* @ngdoc object
* @name ui.router.state.$state
Expand Down Expand Up @@ -613,7 +647,8 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
params: {},
current: root.self,
$current: root,
transition: null
transition: null,
redirects: []
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I put redirects on $state as a shortcut and forgot to move it to a closure variable.

};

/**
Expand Down Expand Up @@ -784,6 +819,11 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
throw new Error("Could not resolve '" + to + "' from state '" + options.relative + "'");
}
}

$state.redirects.push(toState.name);
if ($state.redirects.length >= maximumRedirects)
throw new Error("Redirect count exceeded " + maximumRedirects + ". [ " + $state.redirects.join(" -> ") + " ]");

if (toState[abstractKey]) throw new Error("Cannot transition to abstract state '" + to + "'");
if (options.inherit) toParams = inheritParams($stateParams, toParams || {}, $state.$current, toState);
to = toState;
Expand Down Expand Up @@ -955,6 +995,16 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
$urlRouter.update();
}

return $q.reject(error);
})
.then(function(result) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might prefer this in a transitionTo() decorator (of sorts), instead of this inline-promise-chain.

$state.redirects.pop();
return result;
}, function(error) {
if (error === TransitionSuperseded) {
$state.redirects.push($state.current.name);
return error;
}
return $q.reject(error);
});

Expand Down
16 changes: 16 additions & 0 deletions test/stateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ describe('state', function () {
$state.transitionTo("about");
}
})
.state('home.redirectloop', {
onEnter: function($state) { $state.transitionTo("home.redirectloop.infinite"); }
})
.state('home.redirectloop.infinite', { })
.state('resolveFail', {
url: "/resolve-fail",
resolve: {
Expand Down Expand Up @@ -721,6 +725,8 @@ describe('state', function () {
'home',
'home.item',
'home.redirect',
'home.redirectloop',
'home.redirectloop.infinite',
'resolveFail',
'resolveTimeout',
'root',
Expand Down Expand Up @@ -1015,6 +1021,16 @@ describe('state', function () {
}));

});

describe('infinite loop detection', function () {
it('should detect looping onEnter TransitionSuperceded', inject(function ($q, $state) {
$state.transitionTo('home');
$q.flush();
$state.transitionTo('home.redirectloop');
expect(function() { $q.flush() }).toThrow(new Error("Redirect count exceeded " + stateProvider.maxRedirects() + ". [ home.redirectloop -> home.redirectloop.infinite -> home.redirectloop.infinite -> home.redirectloop.infinite -> home.redirectloop.infinite -> home.redirectloop.infinite -> home.redirectloop.infinite -> home.redirectloop.infinite -> home.redirectloop.infinite -> home.redirectloop.infinite ]") );
expect($state.current.name).toBe('home');
}));
});
});

describe('state queue', function(){
Expand Down