| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- import { VNode } from './vnode'
- import { ComponentInternalInstance, LifecycleHooks } from './component'
- import { warn, pushWarningContext, popWarningContext } from './warning'
- import { isPromise, isFunction } from '@vue/shared'
- // contexts where user provided function may be executed, in addition to
- // lifecycle hooks.
- export const enum ErrorCodes {
- SETUP_FUNCTION,
- RENDER_FUNCTION,
- WATCH_GETTER,
- WATCH_CALLBACK,
- WATCH_CLEANUP,
- NATIVE_EVENT_HANDLER,
- COMPONENT_EVENT_HANDLER,
- DIRECTIVE_HOOK,
- APP_ERROR_HANDLER,
- APP_WARN_HANDLER,
- FUNCTION_REF,
- SCHEDULER
- }
- export const ErrorTypeStrings: Record<number | string, string> = {
- [LifecycleHooks.BEFORE_CREATE]: 'beforeCreate hook',
- [LifecycleHooks.CREATED]: 'created hook',
- [LifecycleHooks.BEFORE_MOUNT]: 'beforeMount hook',
- [LifecycleHooks.MOUNTED]: 'mounted hook',
- [LifecycleHooks.BEFORE_UPDATE]: 'beforeUpdate hook',
- [LifecycleHooks.UPDATED]: 'updated',
- [LifecycleHooks.BEFORE_UNMOUNT]: 'beforeUnmount hook',
- [LifecycleHooks.UNMOUNTED]: 'unmounted hook',
- [LifecycleHooks.ACTIVATED]: 'activated hook',
- [LifecycleHooks.DEACTIVATED]: 'deactivated hook',
- [LifecycleHooks.ERROR_CAPTURED]: 'errorCaptured hook',
- [LifecycleHooks.RENDER_TRACKED]: 'renderTracked hook',
- [LifecycleHooks.RENDER_TRIGGERED]: 'renderTriggered hook',
- [ErrorCodes.SETUP_FUNCTION]: 'setup function',
- [ErrorCodes.RENDER_FUNCTION]: 'render function',
- [ErrorCodes.WATCH_GETTER]: 'watcher getter',
- [ErrorCodes.WATCH_CALLBACK]: 'watcher callback',
- [ErrorCodes.WATCH_CLEANUP]: 'watcher cleanup function',
- [ErrorCodes.NATIVE_EVENT_HANDLER]: 'native event handler',
- [ErrorCodes.COMPONENT_EVENT_HANDLER]: 'component event handler',
- [ErrorCodes.DIRECTIVE_HOOK]: 'directive hook',
- [ErrorCodes.APP_ERROR_HANDLER]: 'app errorHandler',
- [ErrorCodes.APP_WARN_HANDLER]: 'app warnHandler',
- [ErrorCodes.FUNCTION_REF]: 'ref function',
- [ErrorCodes.SCHEDULER]:
- 'scheduler flush. This is likely a Vue internals bug. ' +
- 'Please open an issue at https://new-issue.vuejs.org/?repo=vuejs/vue'
- }
- export type ErrorTypes = LifecycleHooks | ErrorCodes
- export function callWithErrorHandling(
- fn: Function,
- instance: ComponentInternalInstance | null,
- type: ErrorTypes,
- args?: unknown[]
- ) {
- let res
- try {
- res = args ? fn(...args) : fn()
- } catch (err) {
- handleError(err, instance, type)
- }
- return res
- }
- export function callWithAsyncErrorHandling(
- fn: Function | Function[],
- instance: ComponentInternalInstance | null,
- type: ErrorTypes,
- args?: unknown[]
- ) {
- if (isFunction(fn)) {
- const res = callWithErrorHandling(fn, instance, type, args)
- if (res != null && !res._isVue && isPromise(res)) {
- res.catch((err: Error) => {
- handleError(err, instance, type)
- })
- }
- return res
- }
- for (let i = 0; i < fn.length; i++) {
- callWithAsyncErrorHandling(fn[i], instance, type, args)
- }
- }
- export function handleError(
- err: Error,
- instance: ComponentInternalInstance | null,
- type: ErrorTypes
- ) {
- const contextVNode = instance ? instance.vnode : null
- if (instance) {
- let cur = instance.parent
- // the exposed instance is the render proxy to keep it consistent with 2.x
- const exposedInstance = instance.renderProxy
- // in production the hook receives only the error code
- const errorInfo = __DEV__ ? ErrorTypeStrings[type] : type
- while (cur) {
- const errorCapturedHooks = cur.ec
- if (errorCapturedHooks !== null) {
- for (let i = 0; i < errorCapturedHooks.length; i++) {
- if (errorCapturedHooks[i](err, exposedInstance, errorInfo)) {
- return
- }
- }
- }
- cur = cur.parent
- }
- // app-level handling
- const appErrorHandler = instance.appContext.config.errorHandler
- if (appErrorHandler) {
- callWithErrorHandling(
- appErrorHandler,
- null,
- ErrorCodes.APP_ERROR_HANDLER,
- [err, exposedInstance, errorInfo]
- )
- return
- }
- }
- logError(err, type, contextVNode)
- }
- // Test-only toggle for testing the uhandled warning behavior
- let forceRecover = false
- export function setErrorRecovery(value: boolean) {
- forceRecover = value
- }
- function logError(err: Error, type: ErrorTypes, contextVNode: VNode | null) {
- // default behavior is crash in prod & test, recover in dev.
- if (__DEV__ && (forceRecover || !__TEST__)) {
- const info = ErrorTypeStrings[type]
- if (contextVNode) {
- pushWarningContext(contextVNode)
- }
- warn(`Unhandled error${info ? ` during execution of ${info}` : ``}`)
- console.error(err)
- if (contextVNode) {
- popWarningContext()
- }
- } else {
- throw err
- }
- }
|