-
Notifications
You must be signed in to change notification settings - Fork 3k
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
christopherthielen
wants to merge
1
commit into
angular-ui:master
from
christopherthielen:issue-1169
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 = { | ||
|
@@ -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 | ||
|
@@ -613,7 +647,8 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) { | |
params: {}, | ||
current: root.self, | ||
$current: root, | ||
transition: null | ||
transition: null, | ||
redirects: [] | ||
}; | ||
|
||
/** | ||
|
@@ -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; | ||
|
@@ -955,6 +995,16 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) { | |
$urlRouter.update(); | ||
} | ||
|
||
return $q.reject(error); | ||
}) | ||
.then(function(result) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
}); | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.