Skip to content

Commit

Permalink
feat(errors): capture errors thrown in redirect callback in onError (#…
Browse files Browse the repository at this point in the history
…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]>
  • Loading branch information
raulcabello and Raul Cabello authored Jul 3, 2020
1 parent f0d9c2d commit 40e4df7
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/history/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,17 @@ export class History {
onComplete?: Function,
onAbort?: Function
) {
const route = this.router.match(location, this.current)
let route
try {
route = this.router.match(location, this.current)
} catch (e) {
this.errorCbs.forEach(cb => {
cb(e)
})
// Exception should still be thrown
// https://github.com/vuejs/vue-router/issues/3201
throw e
}
this.confirmTransition(
route,
() => {
Expand Down
29 changes: 29 additions & 0 deletions test/unit/specs/error-handling.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,33 @@ describe('error handling', () => {
done()
})
})

it('should trigger onError when an exception is thrown', done => {
const config = [{
path: '/oldpath/:part',
redirect: (to) => {
if (to.ooopsmistake.part) {
return `/newpath/${to.params.part}`
}
return '/newpath/'
}
}]

const router = new VueRouter({
routes: config
})

const onError = jasmine.createSpy('onError')
router.onError(onError)
const pushCatch = jasmine.createSpy('pushCatch')

router
.push('/oldpath/test')
.catch(pushCatch)
.finally(() => {
expect(pushCatch).toHaveBeenCalled()
expect(onError).toHaveBeenCalled()
done()
})
})
})

0 comments on commit 40e4df7

Please sign in to comment.