Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/core/util/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { inBrowser } from './env'
import { isPromise } from 'shared/util'
import { pushTarget, popTarget } from '../observer/dep'

let isHandlingError = false

export function handleError(err: Error, vm: any, info: string) {
// Deactivate deps tracking while processing error handler to avoid possible infinite rendering.
// See: https://github.com/vuejs/vuex/issues/1505
Expand Down Expand Up @@ -55,14 +57,21 @@ export function invokeWithErrorHandling(

function globalHandleError(err, vm, info) {
if (config.errorHandler) {
if (isHandlingError) {
logError(err, vm, info)
return
}
try {
isHandlingError = true
return config.errorHandler.call(null, err, vm, info)
} catch (e: any) {
// if the user intentionally throws the original error in the handler,
// do not log it twice
if (e !== err) {
logError(e, null, 'config.errorHandler')
}
} finally {
isHandlingError = false
}
}
logError(err, vm, info)
Expand Down
28 changes: 28 additions & 0 deletions test/unit/features/error-handling.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,34 @@ describe('Error handling', () => {
Vue.config.errorHandler = undefined
})

it('should avoid recursive error handling when errorHandler triggers another error', () => {
const originalHandler = Vue.config.errorHandler
let handlerCalls = 0
Vue.config.errorHandler = (err, instance) => {
handlerCalls++
if (handlerCalls === 1 && instance) {
instance.$emit('boom')
}
}
new Vue({
created() {
this.$on('boom', () => {
throw new Error('error in boom')
})
},
render(h) {
throw new Error('error in render')
},
renderError(h, err) {
return h('div', err.toString())
}
}).$mount()
expect(handlerCalls).toBe(1)
expect('Error in event handler for "boom"').toHaveBeenWarned()
expect('Error: error in boom').toHaveBeenWarned()
Vue.config.errorHandler = originalHandler
})

// event handlers that can throw errors or return rejected promise
;[
['single handler', '<div v-on:click="bork"></div>'],
Expand Down