Skip to content

Access failed ActivatedRouteSnapshot in NavigationError #27626

@dzinxed

Description

@dzinxed

🚀 Access failed ActivatedRouteSnapshot in NavigationError

Relevant Package

This feature request is for @angular/router. Specifically, /packages/router/src/router.ts.

Description

I have the following scenario:

  1. user clicks a link to go to a new page
  2. one of the resolvers for that page fails with a very specific error (let's say, missing permissions)
  3. I listen on NavigationError events
  4. I catch the NavigationError event. It contains the error raised in step 2 and the URL the user was trying to reach.
  5. I'd like to be able to configure (in the route configuration) where to redirect the user when this specific error occurs. But I don't have the handle to ActivatedRouteSnapshot for the page the user was trying to reach, and so I cannot access its "data" property where I wrote the configuration.

A sample route configuration could look like this:

@NgModule({
    ...
    RouterModule.forChild([{
       data: {
           permissionConfig: {...}
}}])})

Describe the solution you'd like

Ideally, I'd like the failed ActivatedRouteSnapshot to be a field of NavigationError. It's important for it to be the "leaf" ActivatedRouteSnapshot, not the root one, as it's in the leaf where I put my permissionConfig.

Describe alternatives you've considered

Currently I'm using the following workaround: I have a "fake" route guard, with a canActivate call that always returns of(true), but remembers its arguments (ActivatedRouteSnapshot and RouterStateSnapshot) on "global" variables that I can access later when handling the NavigationError elsewhere. I'm also clearing this global state every NavigationStart so that there's no risk of using the wrong snapshot later.

The downside of the workaround is that I have to add the fake guard to every @NgModule that uses permissionConfig. If I forget to do it, it doesn't work. And I cannot add my fake guard to the root @NgModule (to RouterModule.forRoot), because then the ActivatedRouteSnapshot I receive is the root snapshot and I have no means of knowing which of its children I should descend to to find my configuration (there can be multiple children because of outlets).

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions