Skip to content

Commit

Permalink
Add handleError during event handling
Browse files Browse the repository at this point in the history
Currently handleError is used to handle errors during lifecycle hooks.
This commit adds this functionality in to the event handling for
consistency.
  • Loading branch information
chrisnicola committed May 19, 2017
1 parent 43485fb commit ded4a13
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
8 changes: 6 additions & 2 deletions src/core/instance/events.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* @flow */

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

export function initEvents (vm: Component) {
vm._events = Object.create(null)
Expand Down Expand Up @@ -121,7 +121,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 ded4a13

Please sign in to comment.