From e84a3017391624a02c6c8c2746acd593c69f52dd Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Wed, 6 Jul 2022 15:11:01 -0700 Subject: [PATCH] feat(router): Add the target `RouterStateSnapshot` to `NavigationError` This commit adds the target `RouterStateSnapshot` to the `NavigationError` so error handlers/subscribers can more easily determine which navigation failed, including the matched `Route` configs for the navigation. This information was previously not available (neither in `NavigationError` nor the `Router#getCurrentNavigation()`). fixes #27626 --- goldens/public-api/router/index.md | 4 +++- packages/router/src/events.ts | 9 ++++++++- packages/router/src/router.ts | 5 +++-- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/goldens/public-api/router/index.md b/goldens/public-api/router/index.md index 979d53735c4..ba6341cfc93 100644 --- a/goldens/public-api/router/index.md +++ b/goldens/public-api/router/index.md @@ -388,9 +388,11 @@ export class NavigationError extends RouterEvent { constructor( id: number, url: string, - error: any); + error: any, + target?: RouterStateSnapshot | undefined); // (undocumented) error: any; + readonly target?: RouterStateSnapshot | undefined; // (undocumented) toString(): string; // (undocumented) diff --git a/packages/router/src/events.ts b/packages/router/src/events.ts index 532643408c8..1bea49500e3 100644 --- a/packages/router/src/events.ts +++ b/packages/router/src/events.ts @@ -244,7 +244,14 @@ export class NavigationError extends RouterEvent { /** @docsNotRequired */ url: string, /** @docsNotRequired */ - public error: any) { + public error: any, + /** + * The target of the navigation when the error occurred. + * + * Note that this can be `undefined` because an error could have occurred before the + * `RouterStateSnapshot` was created for the navigation. + */ + readonly target?: RouterStateSnapshot) { super(id, url); } diff --git a/packages/router/src/router.ts b/packages/router/src/router.ts index dbc529113e6..6be82753ba2 100644 --- a/packages/router/src/router.ts +++ b/packages/router/src/router.ts @@ -970,8 +970,9 @@ export class Router { * the pre-error state. */ } else { this.restoreHistory(t, true); - const navError = - new NavigationError(t.id, this.serializeUrl(t.extractedUrl), e); + const navError = new NavigationError( + t.id, this.serializeUrl(t.extractedUrl), e, + t.targetSnapshot ?? undefined); eventsSubject.next(navError); try { t.resolve(this.errorHandler(e));