Skip to content

Commit a4ce273

Browse files
atscottthePunderWoman
authored andcommitted
feat(router): Add the target RouterStateSnapshot to NavigationError (#46731)
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 PR Close #46731
1 parent 350e364 commit a4ce273

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

goldens/public-api/router/index.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,9 +388,11 @@ export class NavigationError extends RouterEvent {
388388
constructor(
389389
id: number,
390390
url: string,
391-
error: any);
391+
error: any,
392+
target?: RouterStateSnapshot | undefined);
392393
// (undocumented)
393394
error: any;
395+
readonly target?: RouterStateSnapshot | undefined;
394396
// (undocumented)
395397
toString(): string;
396398
// (undocumented)

packages/router/src/events.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,14 @@ export class NavigationError extends RouterEvent {
244244
/** @docsNotRequired */
245245
url: string,
246246
/** @docsNotRequired */
247-
public error: any) {
247+
public error: any,
248+
/**
249+
* The target of the navigation when the error occurred.
250+
*
251+
* Note that this can be `undefined` because an error could have occurred before the
252+
* `RouterStateSnapshot` was created for the navigation.
253+
*/
254+
readonly target?: RouterStateSnapshot) {
248255
super(id, url);
249256
}
250257

packages/router/src/router.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -970,8 +970,9 @@ export class Router {
970970
* the pre-error state. */
971971
} else {
972972
this.restoreHistory(t, true);
973-
const navError =
974-
new NavigationError(t.id, this.serializeUrl(t.extractedUrl), e);
973+
const navError = new NavigationError(
974+
t.id, this.serializeUrl(t.extractedUrl), e,
975+
t.targetSnapshot ?? undefined);
975976
eventsSubject.next(navError);
976977
try {
977978
t.resolve(this.errorHandler(e));

0 commit comments

Comments
 (0)