Skip to content

Access failed ActivatedRouteSnapshot in NavigationError聽#27626

Closed

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).

Activity

  1. added this to the Backlog milestone on Dec 12, 2018
  2. self-assigned this
    on Dec 12, 2018
  3. angular-robot commented on Jun 4, 2021

    @angular-robot
    Contributor

    Just a heads up that we kicked off a community voting process for your feature request. There are 20 days until the voting process ends.

    Find more details about Angular's feature request process in our documentation.

  4. angular-robot commented on Jun 27, 2021

    @angular-robot
    Contributor

    Thank you for submitting your feature request! Looks like during the polling process it didn't collect a sufficient number of votes to move to the next stage.

    We want to keep Angular rich and ergonomic and at the same time be mindful about its scope and learning journey. If you think your request could live outside Angular's scope, we'd encourage you to collaborate with the community on publishing it as an open source package.

    You can find more details about the feature request process in our documentation.

  5. assigned and unassigned on Jul 1, 2022
  6. angular-automatic-lock-bot commented on Aug 8, 2022

    @angular-automatic-lock-bot

    This issue has been automatically locked due to inactivity.
    Please file a new issue if you are encountering a similar or related problem.

    Read more about our automatic conversation locking policy.

    This action has been performed automatically by a bot.

  7. locked and limited conversation to collaborators on Aug 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions