]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
fix(errors): avoid unhandled errors in history listener
authorEduardo San Martin Morote <posva13@gmail.com>
Tue, 11 Jun 2019 15:38:37 +0000 (17:38 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Tue, 11 Jun 2019 15:38:37 +0000 (17:38 +0200)
src/router.ts

index d603553a1ef68527a4e555bbd2ac2fcbd0253c45..b8f6c7a8c987b0543a084668e1fab27192da001b 100644 (file)
@@ -61,7 +61,10 @@ export class Router {
 
         // a more recent navigation took place
         if (this.pendingLocation !== toLocation) {
-          throw new NavigationCancelled(toLocation, this.currentRoute)
+          return this.triggerError(
+            new NavigationCancelled(toLocation, this.currentRoute),
+            false
+          )
         }
 
         // accept current navigation
@@ -71,20 +74,19 @@ export class Router {
         }
         this.updateReactiveRoute()
       } catch (error) {
-        // TODO: use the push/replace technique with any navigation to
-        // preserve history when moving forward
         if (error instanceof NavigationGuardRedirect) {
           // TODO: refactor the duplication of new NavigationCancelled by
           // checking instanceof NavigationError (it's another TODO)
           // a more recent navigation took place
           if (this.pendingLocation !== toLocation) {
             return this.triggerError(
-              new NavigationCancelled(toLocation, this.currentRoute)
+              new NavigationCancelled(toLocation, this.currentRoute),
+              false
             )
           }
 
-          // TODO: handle errors
-          this.push(error.to)
+          // TODO: tests
+          this.push(error.to).catch(error => this.triggerError(error, false))
         } else if (error instanceof NavigationAborted) {
           // TODO: test on different browsers ensure consistent behavior
           if (info.direction === NavigationDirection.back) {
@@ -96,8 +98,7 @@ export class Router {
             this.history.back(false)
           }
         } else {
-          // TODO: write test
-          this.triggerError(error)
+          this.triggerError(error, false)
         }
       }
     })
@@ -241,7 +242,7 @@ export class Router {
           throw new NavigationCancelled(toLocation, this.currentRoute)
         }
 
-        throw error
+        this.triggerError(error)
       }
     }
 
@@ -388,11 +389,13 @@ export class Router {
   /**
    * Trigger all registered error handlers
    * @param error thrown error
+   * @param shouldThrow set to false to not throw the error
    */
-  private triggerError(error: any): void {
+  private triggerError(error: any, shouldThrow: boolean = true): void {
     for (const handler of this.errorHandlers) {
       handler(error)
     }
+    if (shouldThrow) throw error
   }
 
   private updateReactiveRoute() {