Skip to content

Commit c4133d6

Browse files
authored
fix(error.js): do not console.error handled exceptions (#1761)
When an exception is handled in an `errorHandler` function, do not re-raise it. fix #1760
1 parent 08304bc commit c4133d6

File tree

2 files changed

+63
-2
lines changed

2 files changed

+63
-2
lines changed

Diff for: packages/test-utils/src/error.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ function errorHandler(errorOrString, vm, info) {
2222
vm._error = error
2323
}
2424

25+
if (!instancedErrorHandlers.length) {
26+
throw error
27+
}
2528
// should be one error handler, as only once can be registered with local vue
2629
// regardless, if more exist (for whatever reason), invoke the other user defined error handlers
2730
instancedErrorHandlers.forEach(instancedErrorHandler => {
2831
instancedErrorHandler(error, vm, info)
2932
})
30-
31-
throw error
3233
}
3334

3435
export function throwIfInstancesThrew(vm) {

Diff for: test/specs/create-local-vue.spec.js

+60
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,66 @@ describeWithShallowAndMount('createLocalVue', mountingMethod => {
151151
}
152152
)
153153

154+
itSkipIf(
155+
vueVersion < 2.6,
156+
'Exception suppresed in `errorHandler` is not logged to console.error',
157+
async () => {
158+
const component = Vue.component('TestComponent', {
159+
template: '<button id="btn" @click="clickHandler">Click me</button>',
160+
methods: {
161+
clickHandler() {
162+
throw new Error('Should not be logged')
163+
}
164+
}
165+
})
166+
const errorHandler = jest.fn()
167+
const localVue = createLocalVue({
168+
errorHandler
169+
})
170+
const wrapper = mountingMethod(component, { localVue })
171+
await wrapper.vm.$nextTick()
172+
173+
const { error } = global.console
174+
const spy = jest.spyOn(global.console, 'error')
175+
await wrapper.trigger('click')
176+
global.console.error = error
177+
expect(spy).not.toHaveBeenCalled()
178+
}
179+
)
180+
181+
itSkipIf(
182+
vueVersion < 2.6,
183+
'Exception raised in `errorHandler` bubbles up',
184+
async () => {
185+
const component = Vue.component('TestComponent', {
186+
template: '<button id="btn" @click="clickHandler">Click me</button>',
187+
methods: {
188+
clickHandler() {
189+
throw new Error()
190+
}
191+
}
192+
})
193+
const errorHandler = (err, vm, info) => {
194+
if (err) {
195+
throw new Error('An error that should log')
196+
}
197+
}
198+
const localVue = createLocalVue({
199+
errorHandler
200+
})
201+
const wrapper = mountingMethod(component, { localVue })
202+
await wrapper.vm.$nextTick()
203+
204+
const { error } = global.console
205+
const spy = jest.spyOn(global.console, 'error')
206+
await wrapper.trigger('click')
207+
global.console.error = error
208+
expect(spy).toHaveBeenCalledWith(
209+
'[Vue warn]: Error in config.errorHandler: "Error: An error that should log"'
210+
)
211+
}
212+
)
213+
154214
itSkipIf(
155215
process.env.TEST_ENV === 'browser' || vueVersion < 2.6,
156216
'Calls `errorHandler` when an error is thrown asynchronously',

0 commit comments

Comments
 (0)