8000 feat: better wording for navigation redirected failure · vuejs/vue-router@1f3aea6 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1f3aea6

Browse files
committed
feat: better wording for navigation redirected failure
1 parent dab86c5 commit 1f3aea6

File tree

3 files changed

+59
-36
lines changed

3 files changed

+59
-36
lines changed

examples/router-errors/app.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ const router = new VueRouter({
1717
]
1818
})
1919

20+
window.router = router
21+
2022
router.beforeEach((to, from, next) => {
2123
console.log('from', from.fullPath)
2224
console.log('going to', to.fullPath)

src/history/errors.js

Lines changed: 9 additions & 3 deletions
8000
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ export function createNavigationRedirectedError (from, to) {
1010
from,
1111
to,
1212
NavigationFailureType.redirected,
13-
`Redirected from "${from.fullPath}" to "${stringifyRoute(to)}" via a navigation guard.`
13+
`Redirected when going from "${from.fullPath}" to "${stringifyRoute(
14+
to
15+
)}" via a navigation guard.`
1416
)
1517
}
1618

@@ -28,7 +30,9 @@ export function createNavigationCancelledError (from, to) {
2830
from,
2931
to,
3032
NavigationFailureType.cancelled,
31-
`Navigation cancelled from "${from.fullPath}" to "${to.fullPath}" with a new navigation.`
33+
`Navigation cancelled from "${from.fullPath}" to "${
34+
to.fullPath
35+
}" with a new navigation.`
3236
)
3337
}
3438

@@ -37,7 +41,9 @@ export function createNavigationAbortedError (from, to) {
3741
from,
3842
to,
3943
NavigationFailureType.aborted,
40-
`Navigation aborted from "${from.fullPath}" to "${to.fullPath}" via a navigation guard.`
44+
`Navigation aborted from "${from.fullPath}" to "${
45+
to.fullPath
46+
}" via a navigation guard.`
4147
)
4248
}
4349

test/unit/specs/error-handling.spec.js

Lines changed: 48 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,25 @@ describe('error handling', () => {
88
it('onReady errors', done => {
99
const router = new VueRouter()
1010
const err = new Error('foo')
11-
router.beforeEach(() => { throw err })
12-
router.onError(() => { })
11+
router.beforeEach(() => {
12+
throw err
13+
})
14+
router.onError(() => {})
1315

1416
const onReady = jasmine.createSpy('ready')
1517
const onError = jasmine.createSpy('error')
1618
const onPromiseReject = jasmine.createSpy('promise reject')
1719
router.onReady(onReady, onError)
1820

19-
router.push('/').catch(onPromiseReject).finally(() => {
20-
expect(onReady).not.toHaveBeenCalled()
21-
expect(onError).toHaveBeenCalledWith(err)
22-
expect(onPromiseReject).toHaveBeenCalled()
23-
done()
24-
})
21+
router
22+
.push('/')
23+
.catch(onPromiseReject)
24+
.finally(() => {
25+
expect(onReady).not.toHaveBeenCalled()
26+
expect(onError) 628C .toHaveBeenCalledWith(err)
27+
expect(onPromiseReject).toHaveBeenCalled()
28+
done()
29+
})
2530
})
2631

2732
it('navigation errors', done => {
@@ -32,13 +37,18 @@ describe('error handling', () => {
3237
router.onError(spy)
3338

3439
router.push('/')
35-
router.beforeEach(() => { throw err })
36-
37-
router.push('/foo').catch(spy1).finally(() => {
38-
expect(spy).toHaveBeenCalledWith(err)
39-
expect(spy1).toHaveBeenCalled()
40-
done()
40+
router.beforeEach(() => {
41+
throw err
4142
})
43+
44+
router
45+
.push('/foo')
46+
.catch(spy1)
47+
.finally(() => {
48+
expect(spy).toHaveBeenCalledWith(err)
49+
expect(spy1).toHaveBeenCalled()
50+
done()
51+
})
4252
})
4353

4454
it('NavigationDuplicated error', done => {
@@ -65,17 +75,15 @@ describe('error handling', () => {
6575
router.push('/')
6676
})
6777

68-
it('NavigationCancelled error for nested async navigation', (done) => {
78+
it('NavigationCancelled error for nested async navigation', done => {
6979
const component = {
7080
template: `<img />`,
7181
beforeRouteEnter (to, from, next) {
7282
setTimeout(() => next(), 100)
7383
}
7484
}
7585
const router = new VueRouter({
76-
routes: [
77-
{ path: '/a', component }
78-
]
86+
routes: [{ path: '/a', component }]
7987
})
8088

8189
router.push('/a').catch(err => {
@@ -96,14 +104,18 @@ describe('error handling', () => {
96104

97105
router.push('/foo?redirect=/').catch(err => {
98106
expect(err.type).toBe(NavigationFailureType.redirected)
107+
expect(err.from.path).toBe('/')
108+
expect(err.to.path).toBe('/foo')
99109
done()
100110
})
101111
})
102112

103113
it('NavigationAborted error', done => {
104114
const router = new VueRouter()
105115

106-
router.beforeEach((to, from, next) => { next(false) })
116+
router.beforeEach((to, from, next) => {
117+
next(false)
118+
})
107119

108120
router.push('/foo').catch(err => {
109121
expect(err.type).toBe(NavigationFailureType.aborted)
@@ -115,24 +127,27 @@ describe('error handling', () => {
115127
spyOn(console, 'warn')
116128
const err = new Error('foo')
117129
const spy1 = jasmine.createSpy('error')
118-
const spy2 = jasmine.createSpy('errpr')
130+
const spy2 = jasmine.createSpy('error')
119131
const spy3 = jasmine.createSpy('promise reject')
120-
const Comp = () => { throw err }
132+
const Comp = () => {
133+
throw err
134+
}
121135
const router = new VueRouter({
122-
routes: [
123-
{ path: '/', component: Comp }
124-
]
136+
routes: [{ path: '/', component: Comp }]
125137
})
126138

127139
router.onError(spy1)
128-
router.onReady(() => { }, spy2)
129-
130-
router.push('/').catch(spy3).finally(() => {
131-
expect(spy1).toHaveBeenCalledWith(err)
132-
expect(spy2).toHaveBeenCalledWith(err)
133-
expect(spy3).toHaveBeenCalled()
134-
expect(console.warn).toHaveBeenCalledTimes(1)
135-
done()
136-
})
140+
router.onReady(() => {}, spy2)
141+
142+
router
143+
.push('/')
144+
.catch(spy3)
145+
.finally(() => {
146+
expect(spy1).toHaveBeenCalledWith(err)
147+
expect(spy2).toHaveBeenCalledWith(err)
148+
expect(spy3).toHaveBeenCalled()
149+
expect(console.warn).toHaveBeenCalledTimes(1)
150+
done()
151+
})
137152
})
138153
})

0 commit comments

Comments
 (0)
0