Skip to content

Commit

Permalink
fix(error.js): do not console.error handled exceptions (#1761)
Browse files Browse the repository at this point in the history
When an exception is handled in an `errorHandler` function, do not re-raise it.

fix #1760
  • Loading branch information
marcgibbons committed Jan 10, 2021
1 parent 08304bc commit c4133d6
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
5 changes: 3 additions & 2 deletions packages/test-utils/src/error.js
Expand Up @@ -22,13 +22,14 @@ function errorHandler(errorOrString, vm, info) {
vm._error = error
}

if (!instancedErrorHandlers.length) {
throw error
}
// should be one error handler, as only once can be registered with local vue
// regardless, if more exist (for whatever reason), invoke the other user defined error handlers
instancedErrorHandlers.forEach(instancedErrorHandler => {
instancedErrorHandler(error, vm, info)
})

throw error
}

export function throwIfInstancesThrew(vm) {
Expand Down
60 changes: 60 additions & 0 deletions test/specs/create-local-vue.spec.js
Expand Up @@ -151,6 +151,66 @@ describeWithShallowAndMount('createLocalVue', mountingMethod => {
}
)

itSkipIf(
vueVersion < 2.6,
'Exception suppresed in `errorHandler` is not logged to console.error',
async () => {
const component = Vue.component('TestComponent', {
template: '<button id="btn" @click="clickHandler">Click me</button>',
methods: {
clickHandler() {
throw new Error('Should not be logged')
}
}
})
const errorHandler = jest.fn()
const localVue = createLocalVue({
errorHandler
})
const wrapper = mountingMethod(component, { localVue })
await wrapper.vm.$nextTick()

const { error } = global.console
const spy = jest.spyOn(global.console, 'error')
await wrapper.trigger('click')
global.console.error = error
expect(spy).not.toHaveBeenCalled()
}
)

itSkipIf(
vueVersion < 2.6,
'Exception raised in `errorHandler` bubbles up',
async () => {
const component = Vue.component('TestComponent', {
template: '<button id="btn" @click="clickHandler">Click me</button>',
methods: {
clickHandler() {
throw new Error()
}
}
})
const errorHandler = (err, vm, info) => {
if (err) {
throw new Error('An error that should log')
}
}
const localVue = createLocalVue({
errorHandler
})
const wrapper = mountingMethod(component, { localVue })
await wrapper.vm.$nextTick()

const { error } = global.console
const spy = jest.spyOn(global.console, 'error')
await wrapper.trigger('click')
global.console.error = error
expect(spy).toHaveBeenCalledWith(
'[Vue warn]: Error in config.errorHandler: "Error: An error that should log"'
)
}
)

itSkipIf(
process.env.TEST_ENV === 'browser' || vueVersion < 2.6,
'Calls `errorHandler` when an error is thrown asynchronously',
Expand Down

0 comments on commit c4133d6

Please sign in to comment.