Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
2 changes: 2 additions & 0 deletions goldens/public-api/router/errors.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export const enum RuntimeErrorCode {
// (undocumented)
EMPTY_PATH_WITH_PARAMS = 4009,
// (undocumented)
ERROR_PARSING_URL = 4018,
// (undocumented)
FOR_ROOT_CALLED_TWICE = 4007,
// (undocumented)
INFINITE_REDIRECT = 4016,
Expand Down
1 change: 1 addition & 0 deletions packages/router/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ export const enum RuntimeErrorCode {
INVALID_ROOT_URL_SEGMENT = 4015,
INFINITE_REDIRECT = 4016,
INVALID_ROUTER_LINK_INPUTS = 4017,
ERROR_PARSING_URL = 4018,
}
9 changes: 8 additions & 1 deletion packages/router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
Type,
untracked,
ɵINTERNAL_APPLICATION_ERROR_HANDLER,
ɵformatRuntimeError as formatRuntimeError,
} from '@angular/core';
import {Observable, Subject, Subscription, SubscriptionLike} from 'rxjs';

Expand Down Expand Up @@ -581,7 +582,13 @@ export class Router {
parseUrl(url: string): UrlTree {
try {
return this.urlSerializer.parse(url);
} catch {
} catch (e) {
this.console.warn(
formatRuntimeError(
RuntimeErrorCode.ERROR_PARSING_URL,
ngDevMode && `Error parsing URL ${url}. Falling back to '/' instead. \n` + e,
),
);
return this.urlSerializer.parse('/');
}
}
Expand Down
28 changes: 26 additions & 2 deletions packages/router/test/router.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import {Location} from '@angular/common';
import {EnvironmentInjector, inject} from '@angular/core';
import {EnvironmentInjector, inject, ɵConsole as Console} from '@angular/core';
import {TestBed} from '@angular/core/testing';
import {RouterModule} from '../index';
import {of} from 'rxjs';
Expand All @@ -20,7 +20,7 @@ import {resolveData as resolveDataOperator} from '../src/operators/resolve_data'
import {Router} from '../src/router';
import {ChildrenOutletContexts} from '../src/router_outlet_context';
import {createEmptyStateSnapshot, RouterStateSnapshot} from '../src/router_state';
import {DefaultUrlSerializer, UrlTree} from '../src/url_tree';
import {DefaultUrlSerializer, UrlSerializer, UrlTree} from '../src/url_tree';
import {getAllRouteGuards} from '../src/utils/preactivation';
import {TreeNode} from '../src/utils/tree';

Expand Down Expand Up @@ -108,6 +108,30 @@ describe('Router', () => {
});
});

describe('parseUrl', () => {
beforeEach(() => {
TestBed.configureTestingModule({imports: [RouterModule.forRoot([])]});
});

it('should log a warning and fall back to "/" when parsing fails', () => {
const router: Router = TestBed.inject(Router);
const urlSerializer: UrlSerializer = TestBed.inject(UrlSerializer);
const console: Console = TestBed.inject(Console);
spyOn(urlSerializer, 'parse').and.callFake((url: string) => {
if (url === 'invalid-url') {
throw new Error('test error');
}
// The fallback call should not be mocked
return new DefaultUrlSerializer().parse(url);
});
const spy = spyOn(console, 'warn');

const result = router.parseUrl('invalid-url');
expect(spy.calls.argsFor(0)).toMatch(/Error parsing URL/);
expect(result).toEqual(new DefaultUrlSerializer().parse('/'));
});
});

describe('PreActivation', () => {
const serializer = new DefaultUrlSerializer();
let empty: RouterStateSnapshot;
Expand Down