Skip to content

Commit

Permalink
fix: handle errors in errorHandler
Browse files Browse the repository at this point in the history
close #6714
  • Loading branch information
yyx990803 committed Oct 3, 2017
1 parent ae347a5 commit 2b5c83a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
28 changes: 18 additions & 10 deletions src/core/util/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,24 @@ import { inBrowser } from './env'

export function handleError (err: Error, vm: any, info: string) {
if (config.errorHandler) {
config.errorHandler.call(null, err, vm, info)
} else {
if (process.env.NODE_ENV !== 'production') {
warn(`Error in ${info}: "${err.toString()}"`, vm)
}
/* istanbul ignore else */
if (inBrowser && typeof console !== 'undefined') {
console.error(err)
} else {
throw err
try {
config.errorHandler.call(null, err, vm, info)
return
} catch (e) {
logError(e, null, 'errorHandler')
}
}
logError(err, vm, info)
}

function logError (err, vm, info) {
if (process.env.NODE_ENV !== 'production') {
warn(`Error in ${info}: "${err.toString()}"`, vm)
}
/* istanbul ignore else */
if (inBrowser && typeof console !== 'undefined') {
console.error(err)
} else {
throw err
}
}
20 changes: 19 additions & 1 deletion test/unit/features/error-handling.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ describe('Error handling', () => {
}).then(done)
})

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

Expand Down Expand Up @@ -124,6 +124,24 @@ describe('Error handling', () => {
})
})
})

it('should recover from errors thrown in errorHandler itself', () => {
Vue.config.errorHandler = () => {
throw new Error('error in errorHandler ¯\\_(ツ)_/¯')
}
const vm = new Vue({
render (h) {
throw new Error('error in render')
},
renderError (h, err) {
return h('div', err.toString())
}
}).$mount()
expect('error in errorHandler').toHaveBeenWarned()
expect('error in render').toHaveBeenWarned()
expect(vm.$el.textContent).toContain('error in render')
Vue.config.errorHandler = null
})
})

function createErrorTestComponents () {
Expand Down

0 comments on commit 2b5c83a

Please sign in to comment.