Skip to content

Commit

Permalink
fix(router): throw an error when navigate to null/undefined path
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzmitry Shylovich authored and matsko committed Jan 9, 2017
1 parent 8c7e93b commit 46cb04d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
10 changes: 10 additions & 0 deletions modules/@angular/router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,7 @@ export class Router {
*/
navigate(commands: any[], extras: NavigationExtras = {skipLocationChange: false}):
Promise<boolean> {
validateCommands(commands);
if (typeof extras.queryParams === 'object' && extras.queryParams !== null) {
extras.queryParams = this.removeEmptyProps(extras.queryParams);
}
Expand Down Expand Up @@ -1238,3 +1239,12 @@ function getOutlet(outletMap: RouterOutletMap, route: ActivatedRoute): RouterOut
}
return outlet;
}

function validateCommands(commands: string[]): void {
for (let i = 0; i < commands.length; i++) {
const cmd = commands[i];
if (cmd == null) {
throw new Error(`The requested path contains ${cmd} segment at index ${i}`);
}
}
}
11 changes: 11 additions & 0 deletions modules/@angular/router/test/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,17 @@ describe('Integration', () => {
expect(cmp.recordedParams).toEqual([{name: '1'}]);
})));

it('should throw an error when one of the commands is null/undefined',
fakeAsync(inject([Router], (router: Router) => {
createRoot(router, RootCmp);

router.resetConfig([{path: 'query', component: EmptyQueryParamsCmp}]);

expect(() => router.navigate([
undefined, 'query'
])).toThrowError(`The requested path contains undefined segment at index 0`);
})));

it('should push params only when they change', fakeAsync(inject([Router], (router: Router) => {
const fixture = createRoot(router, RootCmp);

Expand Down

0 comments on commit 46cb04d

Please sign in to comment.