Skip to content

Commit

Permalink
Add handleError during event handling (#5709)
Browse files Browse the repository at this point in the history
* Add handleError during event handling

Currently handleError is used to handle errors during lifecycle hooks.
This commit adds this functionality in to the event handling for
consistency.

* style tweak
  • Loading branch information
chrisnicola authored and yyx990803 committed May 29, 2017
1 parent b182ac4 commit 11b7d5d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
14 changes: 12 additions & 2 deletions src/core/instance/events.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
/* @flow */

import {
tip,
toArray,
hyphenate,
handleError,
formatComponentName
} from '../util/index'
import { updateListeners } from '../vdom/helpers/index'
import { toArray, tip, hyphenate, formatComponentName } from '../util/index'

export function initEvents (vm: Component) {
vm._events = Object.create(null)
Expand Down Expand Up @@ -121,7 +127,11 @@ export function eventsMixin (Vue: Class<Component>) {
cbs = cbs.length > 1 ? toArray(cbs) : cbs
const args = toArray(arguments, 1)
for (let i = 0, l = cbs.length; i < l; i++) {
cbs[i].apply(vm, args)
try {
cbs[i].apply(vm, args)
} catch (e) {
handleError(e, vm, `event handler for "${event}"`)
}
}
}
return vm
Expand Down
16 changes: 15 additions & 1 deletion test/unit/features/error-handling.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ describe('Error handling', () => {
['beforeCreate', 'beforeCreate hook'],
['created', 'created hook'],
['beforeMount', 'beforeMount hook'],
['directive bind', 'directive foo bind hook']
['directive bind', 'directive foo bind hook'],
['event', 'event handler for "e"']
].forEach(([type, description]) => {
it(`should recover from errors in ${type}`, done => {
const vm = createTestInstance(components[type])
Expand Down Expand Up @@ -215,6 +216,19 @@ function createErrorTestComponents () {
}
}

// event errors
components.event = {
beforeCreate () {
this.$on('e', () => { throw new Error('event') })
},
mounted () {
this.$emit('e')
},
render (h) {
return h('div')
}
}

return components
}

Expand Down

0 comments on commit 11b7d5d

Please sign in to comment.