| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent dd29713 commit b65dea4
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -597,7 +597,7 @@ export function provideRouter(routes: Routes, ...features: RouterFeatures[]): En | |||
| 597 | 597 | export type QueryParamsHandling = 'merge' | 'preserve' | 'replace' | ''; | |
| 598 | 598 | ||
| 599 | 599 | // @public | |
| 600 | - export class RedirectCommand { | ||
| 600 | + export class RedirectCommand extends Error { | ||
| 601 | 601 | constructor(redirectTo: UrlTree, navigationBehaviorOptions?: NavigationBehaviorOptions | undefined); | |
| 602 | 602 | // (undocumented) | |
| 603 | 603 | readonly navigationBehaviorOptions?: NavigationBehaviorOptions | undefined; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -117,11 +117,14 @@ export type GuardResult = boolean | UrlTree | RedirectCommand; | |||
| 117 | 117 | * | |
| 118 | 118 | * @publicApi | |
| 119 | 119 | */ | |
| 120 | - export class RedirectCommand { | ||
| 120 | + export class RedirectCommand extends Error { | ||
| 121 | 121 | constructor( | |
| 122 | 122 | readonly redirectTo: UrlTree, | |
| 123 | 123 | readonly navigationBehaviorOptions?: NavigationBehaviorOptions, | |
| 124 | - ) {} | ||
| 124 | + ) { | ||
| 125 | + super(); | ||
| 126 | + Object.setPrototypeOf(this, RedirectCommand.prototype); | ||
| 127 | + } | ||
| 125 | 128 | } | |
| 126 | 129 | ||
| 127 | 130 | /** | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -902,6 +902,10 @@ export class NavigationTransitions { | |||
| 902 | 902 | return EMPTY; | |
| 903 | 903 | } | |
| 904 | 904 | ||
| 905 | + if (e instanceof RedirectCommand) { | ||
| 906 | + e = redirectingNavigationError(this.urlSerializer, e); | ||
| 907 | + } | ||
| 908 | + | ||
| 905 | 909 | /* This error type is issued during Redirect, and is handled as a | |
| 906 | 910 | * cancellation rather than an error. */ | |
| 907 | 911 | if (isNavigationCancelingError(e)) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2488,5 +2488,61 @@ export function guardsIntegrationSuite() { | |||
| 2488 | 2488 | await router.navigateByUrl('/a?q=2'); | |
| 2489 | 2489 | expect(resolveCount).toBe(2); | |
| 2490 | 2490 | }); | |
| 2491 | + | ||
| 2492 | + describe('throwing redirect', () => { | ||
| 2493 | + it('should redirect when a guard throws a RedirectCommand', async () => { | ||
| 2494 | + const router = TestBed.inject(Router); | ||
| 2495 | + const location = TestBed.inject(Location); | ||
| 2496 | + const fixture = await createRoot(router, RootCmp); | ||
| 2497 | + | ||
| 2498 | + router.resetConfig([ | ||
| 2499 | + { | ||
| 2500 | + path: 'a', | ||
| 2501 | + canActivate: [ | ||
| 2502 | + () => { | ||
| 2503 | + throw new RedirectCommand(router.parseUrl('/b')); | ||
| 2504 | + }, | ||
| 2505 | + ], | ||
| 2506 | + component: BlankCmp, | ||
| 2507 | + }, | ||
| 2508 | + { | ||
| 2509 | + path: 'b', | ||
| 2510 | + component: BlankCmp, | ||
| 2511 | + }, | ||
| 2512 | + ]); | ||
| 2513 | + | ||
| 2514 | + router.navigateByUrl('/a'); | ||
| 2515 | + await advance(fixture); | ||
| 2516 | + | ||
| 2517 | + expect(location.path()).toEqual('/b'); | ||
| 2518 | + }); | ||
| 2519 | + | ||
| 2520 | + it('should redirect when a resolver throws a RedirectCommand', async () => { | ||
| 2521 | + const router = TestBed.inject(Router); | ||
| 2522 | + const location = TestBed.inject(Location); | ||
| 2523 | + const fixture = await createRoot(router, RootCmp); | ||
| 2524 | + | ||
| 2525 | + router.resetConfig([ | ||
| 2526 | + { | ||
| 2527 | + path: 'a', | ||
| 2528 | + resolve: { | ||
| 2529 | + data: () => { | ||
| 2530 | + throw new RedirectCommand(router.parseUrl('/b')); | ||
| 2531 | + }, | ||
| 2532 | + }, | ||
| 2533 | + component: BlankCmp, | ||
| 2534 | + }, | ||
| 2535 | + { | ||
| 2536 | + path: 'b', | ||
| 2537 | + component: BlankCmp, | ||
| 2538 | + }, | ||
| 2539 | + ]); | ||
| 2540 | + | ||
| 2541 | + router.navigateByUrl('/a'); | ||
| 2542 | + await advance(fixture); | ||
| 2543 | + | ||
| 2544 | + expect(location.path()).toEqual('/b'); | ||
| 2545 | + }); | ||
| 2546 | + }); | ||
| 2491 | 2547 | }); | |
| 2492 | 2548 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments