question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

$router.push() followed by redirect in a route's beforeEnter hook causes uncaught promise rejection

See original GitHub issue

Version

3.1.3

Reproduction link

https://jsfiddle.net/asu3qnry/

Steps to reproduce

To trigger the uncaught promise rejection, click on “/home” and then the button.

What is expected?

No uncaught promise rejection because there are actually no errors generated by the application. It’s simply a navigation ($router.push) that generates a redirect (next('/foo')) in a beforeEnter hook.

What is actually happening?

Console reports: Uncaught (in promise) undefined


Using a router-link to /bar instead of the initial $router.push('/bar') does not produce the error.

Related to #2833 #2881

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:23
  • Comments:18 (3 by maintainers)

github_iconTop GitHub Comments

29reactions
berniegpcommented, Sep 20, 2019

@posva Thanks for the clarification! However, I would argue that a redirection that cancels a navigation should not be a Promise rejection. Since rejection becomes a throw in the async/await model, it should only occur on actual errors. In my opinion, an expected control flow with a redirection should not have to add error handlers to avoid errors. While reading #2881 and the relevant documentation again, I could not find a description of what $router.push() actually resolves to; only that it should resolve (or reject) when navigation is complete.

The example code you gave in #2881 inspired me to create this global push() override:

const router = new VueRouter({ /* ... */ });

const originalPush = router.push;
router.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) {
    return originalPush.call(this, location, onResolve, onReject);
  }
  return originalPush.call(this, location).catch((err) => {
    if (err) {
      // If there really is an error, throw it
      // Should probably be more sophisticated based on the type of err
      return Promise.reject(err);
    }
    // Otherwise resolve to false to indicate the original push call didn't go to its original
    // destination.
    // TODO: return the final route instead of false?
    return Promise.resolve(false);
  });
};

Since the precise resolve/reject behavior of push() doesn’t seem set in stone, would it be possible to resolve to the final route (or false if navigation aborted) instead of rejecting when there is no error?

20reactions
posvacommented, Sep 18, 2019

I already answered this at https://github.com/vuejs/vue-router/issues/2833#issuecomment-532158403

@berniegp unfortunately, this feature request will do nothing to help you with that. It’s is related to the new promise api and errors are stil being improved (there were not before). You can find more at #2881 (comment)

Please, check the related issues, the reasoning has been verbosely explained there already

Read more comments on GitHub >

github_iconTop Results From Across the Web

vue-router — Uncaught (in promise) Error: Redirected from ...
This error is a way for letting that goToB() function know that the router ... The router.push function is returning a Promise (as...
Read more >
Waiting for the result of a Navigation - Vue Router
When using router-link , Vue Router calls router.push to trigger a navigation. ... A navigation guard redirects somewhere else by returning a new...
Read more >
Protecting Vue Routes with Navigation Guards - CSS-Tricks
A common practice that applications use to protect content is to house them under specific routes and build redirect rules that navigate users ......
Read more >
Authentication best practices for Vue - Sqreen Blog
How do I redirect the user after authentication actions (login/logout)? ... reject) => { // The Promise used for router redirect in login....
Read more >
Advanced Routing with Vue and Vue Router: Redirects & Nav ...
One way this could be useful is to ask a user if they meant to navigate away, and cancel the navigation by passing...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found