Skip to content

Commit 40e4df7

Browse files
raulcabelloRaul Cabello
and
Raul Cabello
authored
feat(errors): capture errors thrown in redirect callback in onError (#3251)
* feat(errors): capture errors thrown in redirect callback in onError Resolves #3201 * feat(errors): capture errors thrown in redirect callback in onError try catch just router.match Resolves #3201 * feat(errors): capture errors thrown in redirect callback in onError remove unnecessary if Resolves #3201 Co-authored-by: Raul Cabello <[email protected]>
1 parent f0d9c2d commit 40e4df7

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/history/base.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,17 @@ export class History {
7777
onComplete?: Function,
7878
onAbort?: Function
7979
) {
80-
const route = this.router.match(location, this.current)
80+
let route
81+
try {
82+
route = this.router.match(location, this.current)
83+
} catch (e) {
84+
this.errorCbs.forEach(cb => {
85+
cb(e)
86+
})
87+
// Exception should still be thrown
88+
// https://github.com/vuejs/vue-router/issues/3201
89+
throw e
90+
}
8191
this.confirmTransition(
8292
route,
8393
() => {

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

+29
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,33 @@ describe('error handling', () => {
183183
done()
184184
})
185185
})
186+
187+
it('should trigger onError when an exception is thrown', done => {
188+
const config = [{
189+
path: '/oldpath/:part',
190+
redirect: (to) => {
191+
if (to.ooopsmistake.part) {
192+
return `/newpath/${to.params.part}`
193+
}
194+
return '/newpath/'
195+
}
196+
}]
197+
198+
const router = new VueRouter({
199+
routes: config
200+
})
201+
202+
const onError = jasmine.createSpy('onError')
203+
router.onError(onError)
204+
const pushCatch = jasmine.createSpy('pushCatch')
205+
206+
router
207+
.push('/oldpath/test')
208+
.catch(pushCatch)
209+
.finally(() => {
210+
expect(pushCatch).toHaveBeenCalled()
211+
expect(onError).toHaveBeenCalled()
212+
done()
213+
})
214+
})
186215
})

0 commit comments

Comments
 (0)