Skip to content

Commit ded4a13

Browse files
committed
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.
1 parent 43485fb commit ded4a13

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/core/instance/events.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* @flow */
22

33
import { updateListeners } from '../vdom/helpers/index'
4-
import { toArray, tip, hyphenate, formatComponentName } from '../util/index'
4+
import { toArray, tip, hyphenate, formatComponentName, handleError } from '../util/index'
55

66
export function initEvents (vm: Component) {
77
vm._events = Object.create(null)
@@ -121,7 +121,11 @@ export function eventsMixin (Vue: Class<Component>) {
121121
cbs = cbs.length > 1 ? toArray(cbs) : cbs
122122
const args = toArray(arguments, 1)
123123
for (let i = 0, l = cbs.length; i < l; i++) {
124-
cbs[i].apply(vm, args)
124+
try {
125+
cbs[i].apply(vm, args)
126+
} catch (e) {
127+
handleError(e, vm, `event handler for "${event}"`)
128+
}
125129
}
126130
}
127131
return vm

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ describe('Error handling', () => {
1111
['beforeCreate', 'beforeCreate hook'],
1212
['created', 'created hook'],
1313
['beforeMount', 'beforeMount hook'],
14-
['directive bind', 'directive foo bind hook']
14+
['directive bind', 'directive foo bind hook'],
15+
['event', 'event handler for "e"']
1516
].forEach(([type, description]) => {
1617
it(`should recover from errors in ${type}`, done => {
1718
const vm = createTestInstance(components[type])
@@ -215,6 +216,19 @@ function createErrorTestComponents () {
215216
}
216217
}
217218

219+
// event errors
220+
components.event = {
221+
beforeCreate () {
222+
this.$on('e', () => { throw new Error('event') })
223+
},
224+
mounted () {
225+
this.$emit('e')
226+
},
227+
render (h) {
228+
return h('div')
229+
}
230+
}
231+
218232
return components
219233
}
220234

0 commit comments

Comments
 (0)