Skip to content

Commit 11b7d5d

Browse files
chrisnicolayyx990803
authored andcommitted
Add handleError during event handling (#5709)
* 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
1 parent b182ac4 commit 11b7d5d

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/core/instance/events.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
/* @flow */
22

3+
import {
4+
tip,
5+
toArray,
6+
hyphenate,
7+
handleError,
8+
formatComponentName
9+
} from '../util/index'
310
import { updateListeners } from '../vdom/helpers/index'
4-
import { toArray, tip, hyphenate, formatComponentName } from '../util/index'
511

612
export function initEvents (vm: Component) {
713
vm._events = Object.create(null)
@@ -121,7 +127,11 @@ export function eventsMixin (Vue: Class<Component>) {
121127
cbs = cbs.length > 1 ? toArray(cbs) : cbs
122128
const args = toArray(arguments, 1)
123129
for (let i = 0, l = cbs.length; i < l; i++) {
124-
cbs[i].apply(vm, args)
130+
try {
131+
cbs[i].apply(vm, args)
132+
} catch (e) {
133+
handleError(e, vm, `event handler for "${event}"`)
134+
}
125135
}
126136
}
127137
return vm

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

+15-1
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)