Skip to content

Commit 2b5c83a

Browse files
committed
fix: handle errors in errorHandler
close #6714
1 parent ae347a5 commit 2b5c83a

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

src/core/util/error.js

+18-10
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,24 @@ import { inBrowser } from './env'
66

77
export function handleError (err: Error, vm: any, info: string) {
88
if (config.errorHandler) {
9-
config.errorHandler.call(null, err, vm, info)
10-
} else {
11-
if (process.env.NODE_ENV !== 'production') {
12-
warn(`Error in ${info}: "${err.toString()}"`, vm)
13-
}
14-
/* istanbul ignore else */
15-
if (inBrowser && typeof console !== 'undefined') {
16-
console.error(err)
17-
} else {
18-
throw err
9+
try {
10+
config.errorHandler.call(null, err, vm, info)
11+
return
12+
} catch (e) {
13+
logError(e, null, 'errorHandler')
1914
}
2015
}
16+
logError(err, vm, info)
17+
}
18+
19+
function logError (err, vm, info) {
20+
if (process.env.NODE_ENV !== 'production') {
21+
warn(`Error in ${info}: "${err.toString()}"`, vm)
22+
}
23+
/* istanbul ignore else */
24+
if (inBrowser && typeof console !== 'undefined') {
25+
console.error(err)
26+
} else {
27+
throw err
28+
}
2129
}

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

+19-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ describe('Error handling', () => {
9292
}).then(done)
9393
})
9494

95-
it('config.errorHandler should capture errors', done => {
95+
it('config.errorHandler should capture render errors', done => {
9696
const spy = Vue.config.errorHandler = jasmine.createSpy('errorHandler')
9797
const vm = createTestInstance(components.render)
9898

@@ -124,6 +124,24 @@ describe('Error handling', () => {
124124
})
125125
})
126126
})
127+
128+
it('should recover from errors thrown in errorHandler itself', () => {
129+
Vue.config.errorHandler = () => {
130+
throw new Error('error in errorHandler ¯\\_(ツ)_/¯')
131+
}
132+
const vm = new Vue({
133+
render (h) {
134+
throw new Error('error in render')
135+
},
136+
renderError (h, err) {
137+
return h('div', err.toString())
138+
}
139+
}).$mount()
140+
expect('error in errorHandler').toHaveBeenWarned()
141+
expect('error in render').toHaveBeenWarned()
142+
expect(vm.$el.textContent).toContain('error in render')
143+
Vue.config.errorHandler = null
144+
})
127145
})
128146

129147
function createErrorTestComponents () {

0 commit comments

Comments
 (0)