Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle Promise rejection in guards (fix #2833) #2838

Closed
wants to merge 12 commits into from
Closed
2 changes: 1 addition & 1 deletion .babelrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"presets": ["env", "flow-vue"],
"plugins": ["syntax-dynamic-import"]
"plugins": ["syntax-dynamic-import", "transform-runtime"]
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"babel-eslint": "^10.0.1",
"babel-loader": "^7.1.3",
"babel-plugin-syntax-dynamic-import": "^6.18.0",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-env": "^1.6.1",
"babel-preset-flow-vue": "^1.0.0",
"browserstack-local": "^1.4.0",
Expand Down
4 changes: 2 additions & 2 deletions src/history/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,12 @@ export class History {
)

this.pending = route
const iterator = (hook: NavigationGuard, next) => {
const iterator = async (hook: NavigationGuard, next) => {
if (this.pending !== route) {
return abort()
}
try {
hook(route, current, (to: any) => {
await hook(route, current, (to: any) => {
if (to === false || isError(to)) {
// next(false) -> abort navigation, ensure current URL
this.ensureURL(true)
Expand Down
17 changes: 17 additions & 0 deletions test/unit/specs/error-handling.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,21 @@ describe('error handling', () => {
expect(spy1).toHaveBeenCalledWith(err)
expect(spy2).toHaveBeenCalledWith(err)
})

// #2833
it('async router.beforeEach, handle onError', () => {
const router = new VueRouter()
const err = new Error('foo')
const spy = jasmine.createSpy('error')
router.onError(spy)

router.push('/')
router.beforeEach(async () => { throw err })

router.push('/foo', () => {
fail('onError function did not receive an error')
}, () => {
expect(spy).toHaveBeenCalledWith(err)
})
})
})
7 changes: 7 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2017,6 +2017,13 @@ babel-plugin-transform-regenerator@^6.22.0:
dependencies:
regenerator-transform "^0.10.0"

babel-plugin-transform-runtime@^6.23.0:
version "6.23.0"
resolved "https://registry.yarnpkg.com/babel-plugin-transform-runtime/-/babel-plugin-transform-runtime-6.23.0.tgz#88490d446502ea9b8e7efb0fe09ec4d99479b1ee"
integrity sha1-iEkNRGUC6puOfvsP4J7E2ZR5se4=
dependencies:
babel-runtime "^6.22.0"

babel-plugin-transform-strict-mode@^6.24.1:
version "6.24.1"
resolved "https://registry.yarnpkg.com/babel-plugin-transform-strict-mode/-/babel-plugin-transform-strict-mode-6.24.1.tgz#d5faf7aa578a65bbe591cf5edae04a0c67020758"
Expand Down