Skip to content

bug(Router): Stack trace on unrecognized routes #7349

Closed

Description

@tiziano88

@petebacondarwin @btford @IgorMinar

Code sample: http://plnkr.co/edit/Ly2gfBsyd1J4iJlTjA4C
To reproduce:

Note that navigating directly to the incorrect URL does not seem to trigger the issue; only navigating from a valid to an invalid route in the context of the same tab does.

Attempted fix: #7203

Activity

  1. petebacondarwin commented on Mar 2, 2016

    @petebacondarwin
    Contributor

    OK. This is a valid bug. We should throw a better error on an unmatched route in both cases: navigation from outside the app; navigation within the app.

  2. tiziano88 commented on Mar 2, 2016

    @tiziano88
    Author

    Thanks @petebacondarwin ; is there any way at all to catch the current error? I was not able to find one, which means that this exception always escapes and, in our case, crashes the entire app. Unless a way exists to handle router errors in general, I think throwing a better error would not solve the issue in a meaningful way. Also, I would personally consider this at least a P1 issue, again under the assumption that there is currently no way of catching router errors. If such a way exists, then P2 is appropriate.

  3. petebacondarwin commented on Mar 2, 2016

    @petebacondarwin
    Contributor

    Well in the bigger picture of Angular 2 I am afraid that this is not going to be viewed as urgent - but I appreciate that it needs to be fixed.

    So at the moment we have this bit of code:

    this._locationSub = this._location.subscribe((change) => {
          // we call recognize ourselves
          this.recognize(change['url'])
              .then((instruction) => {
                this.navigateByInstruction(instruction, isPresent(change['pop']))
                    .then((_) => {
                      // this is a popstate event; no need to change the URL
                      if (isPresent(change['pop']) && change['type'] != 'hashchange') {
                        return;
                      }
                      var emitPath = instruction.toUrlPath();
                      var emitQuery = instruction.toUrlQuery();
                      if (emitPath.length > 0 && emitPath[0] != '/') {
                        emitPath = '/' + emitPath;
                      }
    
                      // Because we've opted to use All hashchange events occur outside Angular.
                      // However, apps that are migrating might have hash links that operate outside
                      // angular to which routing must respond.
                      // To support these cases where we respond to hashchanges and redirect as a
                      // result, we need to replace the top item on the stack.
                      if (change['type'] == 'hashchange') {
                        if (instruction.toRootUrl() != this._location.path()) {
                          this._location.replaceState(emitPath, emitQuery);
                        }
                      } else {
                        this._location.go(emitPath, emitQuery);
                      }
                    });
              });

    The subscribe(onNext, onThrow, onReturn) method is only taking a single callback, onNext..
    I think we should probably have an onThrow callback too but I am not sure what we should do with it.

    Perhaps we should change the API of the router.subscribe(onNext) to also allow an onError callback, which we can call if a route change fails?

  4. tiziano88 commented on Mar 2, 2016

    @tiziano88
    Author

    What I am suggesting is that for the time being, it would probably be better to just avoid throwing errors entirely (which is what I propose in #7203 ), rather than throwing unrecoverable ones. When a better mechanism exists for handling them, then a more meaningful error may be thrown instead.

  5. petebacondarwin commented on Mar 2, 2016

    @petebacondarwin
    Contributor

    I think I have a fix almost ready.

  6. petebacondarwin commented on Mar 2, 2016

    @petebacondarwin
    Contributor

    Here you go! I tried it with a local version of your Plunker and there is now no unwanted unhandled exception.

  7. tiziano88 commented on Mar 4, 2016

    @tiziano88
    Author

    Awesome! Thanks for the fix, I'll drop my PR as soon as yours is merged then!

  8. naomiblack commented on Mar 7, 2016

    @naomiblack
    Contributor

    Closing because Pete's fix is merged!

  9. added a commit that references this issue on Mar 10, 2016
    8e3e450
  10. tiziano88 commented on Mar 10, 2016

    @tiziano88
    Author

    Thanks for the fix! I have just tried syncing past it, but it does not seem to completely solve the issue in Dart; in particular, because in Dart it is only possible to subscribe to router updates with cancelOnError implicitly set to true, the router will stop getting updates after the first error.

  11. petebacondarwin commented on Mar 10, 2016

    @petebacondarwin
    Contributor

    @tiziano88 can you create a new issue to track this please

  12. angular-automatic-lock-bot commented on Sep 8, 2019

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

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

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions