Skip to content
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

fix(router): handle unhandled promise rejections with ErrorHandler #51042

Closed
wants to merge 1 commit into from
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
18 changes: 12 additions & 6 deletions packages/router/src/navigation_transition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {EnvironmentInjector, inject, Injectable, Type} from '@angular/core';
import {EnvironmentInjector, ErrorHandler as ErrorHandlerClass, inject, Injectable, Type} from '@angular/core';
import {BehaviorSubject, combineLatest, EMPTY, Observable, of, Subject} from 'rxjs';
import {catchError, defaultIfEmpty, filter, finalize, map, switchMap, take, tap} from 'rxjs/operators';

Expand Down Expand Up @@ -293,6 +293,8 @@ export class NavigationTransitions {
private readonly urlSerializer = inject(UrlSerializer);
private readonly rootContexts = inject(ChildrenOutletContexts);
private readonly inputBindingEnabled = inject(INPUT_BINDER, {optional: true}) !== null;

private readonly errorHandler = inject(ErrorHandlerClass);
navigationId = 0;
get hasRequestedNavigation() {
return this.navigationId !== 0;
Expand Down Expand Up @@ -727,11 +729,15 @@ export class NavigationTransitions {
isBrowserTriggeredNavigation(overallTransitionState.source)
};

router.scheduleNavigation(
mergedTree, IMPERATIVE_NAVIGATION, null, extras, {
resolve: overallTransitionState.resolve,
reject: overallTransitionState.reject,
promise: overallTransitionState.promise
router
.scheduleNavigation(
mergedTree, IMPERATIVE_NAVIGATION, null, extras, {
resolve: overallTransitionState.resolve,
reject: overallTransitionState.reject,
promise: overallTransitionState.promise
})
.catch(e => {
this.errorHandler.handleError(e);
});
}

Expand Down
10 changes: 7 additions & 3 deletions packages/router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import {Location} from '@angular/common';
import {inject, Injectable, NgZone, Type, ɵConsole as Console, ɵInitialRenderPendingTasks as InitialRenderPendingTasks, ɵRuntimeError as RuntimeError} from '@angular/core';
import {ErrorHandler, inject, Injectable, NgZone, Type, ɵConsole as Console, ɵInitialRenderPendingTasks as InitialRenderPendingTasks, ɵRuntimeError as RuntimeError} from '@angular/core';
import {Observable, of, SubscriptionLike} from 'rxjs';

import {createSegmentGroupFromRoute, createUrlTreeFromSegmentGroup} from './create_url_tree';
Expand Down Expand Up @@ -159,6 +159,8 @@ export class Router {
return (this.location.getState() as RestoredState | null)?.ɵrouterPageId;
}
private console = inject(Console);

private errorHandlerInstance = inject(ErrorHandler);
private isNgZoneEnabled: boolean = false;

/**
Expand Down Expand Up @@ -389,7 +391,7 @@ export class Router {
* the Router needs to respond to ensure its internal state matches.
*/
private navigateToSyncWithBrowser(
url: string, source: NavigationTrigger, state: RestoredState|undefined) {
url: string, source: NavigationTrigger, state: RestoredState|undefined): void {
const extras: NavigationExtras = {replaceUrl: true};

// TODO: restoredState should always include the entire state, regardless
Expand All @@ -414,7 +416,9 @@ export class Router {
}

const urlTree = this.parseUrl(url);
this.scheduleNavigation(urlTree, source, restoredState, extras);
this.scheduleNavigation(urlTree, source, restoredState, extras).catch(e => {
this.errorHandlerInstance.handleError(e);
});
}

/** The current URL. */
Expand Down
7 changes: 3 additions & 4 deletions packages/router/test/computed_state_restoration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,10 +431,9 @@ describe('`restoredState#ɵrouterPageId`', () => {

TestBed.inject(ThrowingCanActivateGuard).throw = true;

expect(() => {
location.back();
advance(fixture);
}).toThrow();
location.back();
advance(fixture);

expect(location.path()).toEqual('/second');
expect(location.getState()).toEqual(jasmine.objectContaining({ɵrouterPageId: 2}));

Expand Down