Skip to content

Commit

Permalink
feat(runtime-core): add app.config.throwUnhandledErrorInProduction
Browse files Browse the repository at this point in the history
close #7876
  • Loading branch information
yyx990803 committed Jul 17, 2024
1 parent 9124943 commit f476b7f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 11 deletions.
17 changes: 17 additions & 0 deletions packages/runtime-core/__tests__/apiCreateApp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,23 @@ describe('api: createApp', () => {
expect(serializeInner(root)).toBe('hello')
})

test('config.throwUnhandledErrorInProduction', () => {
__DEV__ = false
try {
const err = new Error()
const app = createApp({
setup() {
throw err
},
})
app.config.throwUnhandledErrorInProduction = true
const root = nodeOps.createElement('div')
expect(() => app.mount(root)).toThrow(err)
} finally {
__DEV__ = true
}
})

test('return property "_" should not overwrite "ctx._", __isScriptSetup: false', () => {
const Comp = defineComponent({
setup() {
Expand Down
7 changes: 7 additions & 0 deletions packages/runtime-core/src/apiCreateApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ export interface AppConfig {
* Enable warnings for computed getters that recursively trigger itself.
*/
warnRecursiveComputed?: boolean

/**
* Whether to throw unhandled errors in production.
* Default is `false` to avoid crashing on any error (and only logs it)
* But in some cases, e.g. SSR, throwing might be more desirable.
*/
throwUnhandledErrorInProduction?: boolean
}

export interface AppContext {
Expand Down
25 changes: 14 additions & 11 deletions packages/runtime-core/src/errorHandling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { pauseTracking, resetTracking } from '@vue/reactivity'
import type { VNode } from './vnode'
import type { ComponentInternalInstance } from './component'
import { popWarningContext, pushWarningContext, warn } from './warning'
import { isArray, isFunction, isPromise } from '@vue/shared'
import { EMPTY_OBJ, isArray, isFunction, isPromise } from '@vue/shared'
import { LifecycleHooks } from './enums'

// contexts where user provided function may be executed, in addition to
Expand Down Expand Up @@ -111,7 +111,9 @@ export function handleError(
type: ErrorTypes,
throwInDev = true,
) {
const contextVNode = instance ? instance.vnode : null
const contextVNode = instance && instance.vnode
const { errorHandler, throwUnhandledErrorInProduction } =
(instance && instance.appContext.config) || EMPTY_OBJ
if (instance) {
let cur = instance.parent
// the exposed instance is the render proxy to keep it consistent with 2.x
Expand All @@ -134,27 +136,26 @@ export function handleError(
cur = cur.parent
}
// app-level handling
const appErrorHandler = instance.appContext.config.errorHandler
if (appErrorHandler) {
if (errorHandler) {
pauseTracking()
callWithErrorHandling(
appErrorHandler,
null,
ErrorCodes.APP_ERROR_HANDLER,
[err, exposedInstance, errorInfo],
)
callWithErrorHandling(errorHandler, null, ErrorCodes.APP_ERROR_HANDLER, [
err,
exposedInstance,
errorInfo,
])
resetTracking()
return
}
}
logError(err, type, contextVNode, throwInDev)
logError(err, type, contextVNode, throwInDev, throwUnhandledErrorInProduction)
}

function logError(
err: unknown,
type: ErrorTypes,
contextVNode: VNode | null,
throwInDev = true,
throwInProd = false,
) {
if (__DEV__) {
const info = ErrorTypeStrings[type]
Expand All @@ -171,6 +172,8 @@ function logError(
} else if (!__TEST__) {
console.error(err)
}
} else if (throwInProd) {
throw err
} else {
// recover in prod to reduce the impact on end-user
console.error(err)
Expand Down

0 comments on commit f476b7f

Please sign in to comment.