Skip to content

Commit 4e14d85

Browse files
committed
refactor: fix extended error usage
1 parent 0ec4713 commit 4e14d85

File tree

5 files changed

+31
-15
lines changed

5 files changed

+31
-15
lines changed

Diff for: src/history/abstract.js

+19-10
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import type Router from '../index'
44
import { History } from './base'
55
import { NavigationDuplicated } from './errors'
6+
import { isExtendedError } from '../util/warn'
67

78
export class AbstractHistory extends History {
89
index: number
@@ -15,18 +16,26 @@ export class AbstractHistory extends History {
1516
}
1617

1718
push (location: RawLocation, onComplete?: Function, onAbort?: Function) {
18-
this.transitionTo(location, route => {
19-
this.stack = this.stack.slice(0, this.index + 1).concat(route)
20-
this.index++
21-
onComplete && onComplete(route)
22-
}, onAbort)
19+
this.transitionTo(
20+
location,
21+
route => {
22+
this.stack = this.stack.slice(0, this.index + 1).concat(route)
23+
this.index++
24+
onComplete && onComplete(route)
25+
},
26+
onAbort
27+
)
2328
}
2429

2530
replace (location: RawLocation, onComplete?: Function, onAbort?: Function) {
26-
this.transitionTo(location, route => {
27-
this.stack = this.stack.slice(0, this.index).concat(route)
28-
onComplete && onComplete(route)
29-
}, onAbort)
31+
this.transitionTo(
32+
location,
33+
route => {
34+
this.stack = this.stack.slice(0, this.index).concat(route)
35+
onComplete && onComplete(route)
36+
},
37+
onAbort
38+
)
3039
}
3140

3241
go (n: number) {
@@ -42,7 +51,7 @@ export class AbstractHistory extends History {
4251
this.updateRoute(route)
4352
},
4453
err => {
45-
if (err instanceof NavigationDuplicated) {
54+
if (isExtendedError(NavigationDuplicated, err)) {
4655
this.index = targetIndex
4756
}
4857
}

Diff for: src/history/base.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { _Vue } from '../install'
44
import type Router from '../index'
55
import { inBrowser } from '../util/dom'
66
import { runQueue } from '../util/async'
7-
import { warn, isError } from '../util/warn'
7+
import { warn, isError, isExtendedError } from '../util/warn'
88
import { START, isSameRoute } from '../util/route'
99
import {
1010
flatten,
@@ -92,9 +92,11 @@ export class History {
9292
// When the user navigates through history through back/forward buttons
9393
// we do not want to throw the error. We only throw it if directly calling
9494
// push/replace. That's why it's not included in isError
95-
if (!(err instanceof NavigationDuplicated) && isError(err)) {
95+
if (!isExtendedError(NavigationDuplicated, err) && isError(err)) {
9696
if (this.errorCbs.length) {
97-
this.errorCbs.forEach(cb => { cb(err) })
97+
this.errorCbs.forEach(cb => {
98+
cb(err)
99+
})
98100
} else {
99101
warn(false, 'uncaught error during route navigation:')
100102
console.error(err)

Diff for: src/history/errors.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
export class NavigationDuplicated extends Error {
22
constructor () {
33
super('Navigating to current location is not allowed')
4-
this.name = this.constructor.name
4+
this.name = 'NavigationDuplicated'
55
}
66
}

Diff for: src/util/warn.js

+4
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,7 @@ export function warn (condition: any, message: string) {
1515
export function isError (err: any): boolean {
1616
return Object.prototype.toString.call(err).indexOf('Error') > -1
1717
}
18+
19+
export function isExtendedError (constructor: Function, err: any): boolean {
20+
return err instanceof constructor || (err && err.name === constructor.name)
21+
}

Diff for: test/unit/specs/node.spec.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ describe('Usage in Node', () => {
5959
router.replace('/bar', success, error)
6060
expect(success).toHaveBeenCalledTimes(4)
6161
expect(error).toHaveBeenCalledTimes(0)
62-
spyOn(console, 'warn')
62+
63+
expect(router.history.current.path).toBe('/bar')
6364
router.back()
6465
expect(router.history.current.path).toBe('/bar')
6566
router.back()

0 commit comments

Comments
 (0)