feat(router): allow throwing RedirectCommand to trigger redirects by atscott · Pull Request #69525 · angular/angular · GitHub
Skip to content
Draft
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 goldens/public-api/router/index.api.md
7 changes: 5 additions & 2 deletions packages/router/src/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,14 @@ export type GuardResult = boolean | UrlTree | RedirectCommand;
*
* @publicApi
*/
export class RedirectCommand {
export class RedirectCommand extends Error {
constructor(
readonly redirectTo: UrlTree,
readonly navigationBehaviorOptions?: NavigationBehaviorOptions,
) {}
) {
super();
Object.setPrototypeOf(this, RedirectCommand.prototype);
}
}

/**
Expand Down
4 changes: 4 additions & 0 deletions packages/router/src/navigation_transition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,10 @@ export class NavigationTransitions {
return EMPTY;
}

if (e instanceof RedirectCommand) {
e = redirectingNavigationError(this.urlSerializer, e);
}

/* This error type is issued during Redirect, and is handled as a
* cancellation rather than an error. */
if (isNavigationCancelingError(e)) {
Expand Down
56 changes: 56 additions & 0 deletions packages/router/test/integration/guards.spec.ts
Loading