Skip to content

Commit 3cc768f

Browse files
committed
refactor(runtime-core): adjust error handling behavior
- Crash in dev to make the errors more noticeable - Recover in prod to reduce impact on end users
1 parent 6783648 commit 3cc768f

File tree

2 files changed

+12
-24
lines changed

2 files changed

+12
-24
lines changed

packages/runtime-core/__tests__/errorHandling.spec.ts

+7-14
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,8 @@ import {
1010
defineComponent,
1111
watchEffect
1212
} from '@vue/runtime-test'
13-
import { setErrorRecovery } from '../src/errorHandling'
1413

1514
describe('error handling', () => {
16-
beforeEach(() => {
17-
setErrorRecovery(true)
18-
})
19-
20-
afterEach(() => {
21-
setErrorRecovery(false)
22-
})
23-
2415
test('propagation', () => {
2516
const err = new Error('foo')
2617
const fn = jest.fn()
@@ -470,8 +461,6 @@ describe('error handling', () => {
470461
})
471462

472463
it('should warn unhandled', () => {
473-
const onError = jest.spyOn(console, 'error')
474-
onError.mockImplementation(() => {})
475464
const groupCollapsed = jest.spyOn(console, 'groupCollapsed')
476465
groupCollapsed.mockImplementation(() => {})
477466
const log = jest.spyOn(console, 'log')
@@ -496,14 +485,18 @@ describe('error handling', () => {
496485
render() {}
497486
}
498487

499-
render(h(Comp), nodeOps.createElement('div'))
488+
let caughtError
489+
try {
490+
render(h(Comp), nodeOps.createElement('div'))
491+
} catch (caught) {
492+
caughtError = caught
493+
}
500494
expect(fn).toHaveBeenCalledWith(err, 'setup function')
501495
expect(
502496
`Unhandled error during execution of setup function`
503497
).toHaveBeenWarned()
504-
expect(onError).toHaveBeenCalledWith(err)
498+
expect(caughtError).toBe(err)
505499

506-
onError.mockRestore()
507500
groupCollapsed.mockRestore()
508501
log.mockRestore()
509502
})

packages/runtime-core/src/errorHandling.ts

+5-10
Original file line numberDiff line numberDiff line change
@@ -134,25 +134,20 @@ export function handleError(
134134
logError(err, type, contextVNode)
135135
}
136136

137-
// Test-only toggle for testing the unhandled warning behavior
138-
let forceRecover = false
139-
export function setErrorRecovery(value: boolean) {
140-
forceRecover = value
141-
}
142-
143137
function logError(err: unknown, type: ErrorTypes, contextVNode: VNode | null) {
144-
// default behavior is crash in prod & test, recover in dev.
145-
if (__DEV__ && (forceRecover || !__TEST__)) {
138+
if (__DEV__) {
146139
const info = ErrorTypeStrings[type]
147140
if (contextVNode) {
148141
pushWarningContext(contextVNode)
149142
}
150143
warn(`Unhandled error${info ? ` during execution of ${info}` : ``}`)
151-
console.error(err)
152144
if (contextVNode) {
153145
popWarningContext()
154146
}
155-
} else {
147+
// crash in dev so it's more noticeable
156148
throw err
149+
} else {
150+
// recover in prod to reduce the impact on end-user
151+
console.error(err)
157152
}
158153
}

0 commit comments

Comments
 (0)