errorHandling.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import { VNode } from './vnode'
  2. import { ComponentInternalInstance, LifecycleHooks } from './component'
  3. import { warn, pushWarningContext, popWarningContext } from './warning'
  4. import { isPromise, isFunction } from '@vue/shared'
  5. // contexts where user provided function may be executed, in addition to
  6. // lifecycle hooks.
  7. export const enum ErrorCodes {
  8. SETUP_FUNCTION,
  9. RENDER_FUNCTION,
  10. WATCH_GETTER,
  11. WATCH_CALLBACK,
  12. WATCH_CLEANUP,
  13. NATIVE_EVENT_HANDLER,
  14. COMPONENT_EVENT_HANDLER,
  15. VNODE_HOOK,
  16. DIRECTIVE_HOOK,
  17. TRANSITION_HOOK,
  18. APP_ERROR_HANDLER,
  19. APP_WARN_HANDLER,
  20. FUNCTION_REF,
  21. ASYNC_COMPONENT_LOADER,
  22. SCHEDULER
  23. }
  24. export const ErrorTypeStrings: Record<number | string, string> = {
  25. [LifecycleHooks.SERVER_PREFETCH]: 'serverPrefetch hook',
  26. [LifecycleHooks.BEFORE_CREATE]: 'beforeCreate hook',
  27. [LifecycleHooks.CREATED]: 'created hook',
  28. [LifecycleHooks.BEFORE_MOUNT]: 'beforeMount hook',
  29. [LifecycleHooks.MOUNTED]: 'mounted hook',
  30. [LifecycleHooks.BEFORE_UPDATE]: 'beforeUpdate hook',
  31. [LifecycleHooks.UPDATED]: 'updated',
  32. [LifecycleHooks.BEFORE_UNMOUNT]: 'beforeUnmount hook',
  33. [LifecycleHooks.UNMOUNTED]: 'unmounted hook',
  34. [LifecycleHooks.ACTIVATED]: 'activated hook',
  35. [LifecycleHooks.DEACTIVATED]: 'deactivated hook',
  36. [LifecycleHooks.ERROR_CAPTURED]: 'errorCaptured hook',
  37. [LifecycleHooks.RENDER_TRACKED]: 'renderTracked hook',
  38. [LifecycleHooks.RENDER_TRIGGERED]: 'renderTriggered hook',
  39. [ErrorCodes.SETUP_FUNCTION]: 'setup function',
  40. [ErrorCodes.RENDER_FUNCTION]: 'render function',
  41. [ErrorCodes.WATCH_GETTER]: 'watcher getter',
  42. [ErrorCodes.WATCH_CALLBACK]: 'watcher callback',
  43. [ErrorCodes.WATCH_CLEANUP]: 'watcher cleanup function',
  44. [ErrorCodes.NATIVE_EVENT_HANDLER]: 'native event handler',
  45. [ErrorCodes.COMPONENT_EVENT_HANDLER]: 'component event handler',
  46. [ErrorCodes.VNODE_HOOK]: 'vnode hook',
  47. [ErrorCodes.DIRECTIVE_HOOK]: 'directive hook',
  48. [ErrorCodes.TRANSITION_HOOK]: 'transition hook',
  49. [ErrorCodes.APP_ERROR_HANDLER]: 'app errorHandler',
  50. [ErrorCodes.APP_WARN_HANDLER]: 'app warnHandler',
  51. [ErrorCodes.FUNCTION_REF]: 'ref function',
  52. [ErrorCodes.ASYNC_COMPONENT_LOADER]: 'async component loader',
  53. [ErrorCodes.SCHEDULER]:
  54. 'scheduler flush. This is likely a Vue internals bug. ' +
  55. 'Please open an issue at https://new-issue.vuejs.org/?repo=vuejs/vue-next'
  56. }
  57. export type ErrorTypes = LifecycleHooks | ErrorCodes
  58. export function callWithErrorHandling(
  59. fn: Function,
  60. instance: ComponentInternalInstance | null,
  61. type: ErrorTypes,
  62. args?: unknown[]
  63. ) {
  64. let res
  65. try {
  66. res = args ? fn(...args) : fn()
  67. } catch (err) {
  68. handleError(err, instance, type)
  69. }
  70. return res
  71. }
  72. export function callWithAsyncErrorHandling(
  73. fn: Function | Function[],
  74. instance: ComponentInternalInstance | null,
  75. type: ErrorTypes,
  76. args?: unknown[]
  77. ): any[] {
  78. if (isFunction(fn)) {
  79. const res = callWithErrorHandling(fn, instance, type, args)
  80. if (res && isPromise(res)) {
  81. res.catch(err => {
  82. handleError(err, instance, type)
  83. })
  84. }
  85. return res
  86. }
  87. const values = []
  88. for (let i = 0; i < fn.length; i++) {
  89. values.push(callWithAsyncErrorHandling(fn[i], instance, type, args))
  90. }
  91. return values
  92. }
  93. export function handleError(
  94. err: unknown,
  95. instance: ComponentInternalInstance | null,
  96. type: ErrorTypes,
  97. throwInDev = true
  98. ) {
  99. const contextVNode = instance ? instance.vnode : null
  100. if (instance) {
  101. let cur = instance.parent
  102. // the exposed instance is the render proxy to keep it consistent with 2.x
  103. const exposedInstance = instance.proxy
  104. // in production the hook receives only the error code
  105. const errorInfo = __DEV__ ? ErrorTypeStrings[type] : type
  106. while (cur) {
  107. const errorCapturedHooks = cur.ec
  108. if (errorCapturedHooks) {
  109. for (let i = 0; i < errorCapturedHooks.length; i++) {
  110. if (
  111. errorCapturedHooks[i](err, exposedInstance, errorInfo) === false
  112. ) {
  113. return
  114. }
  115. }
  116. }
  117. cur = cur.parent
  118. }
  119. // app-level handling
  120. const appErrorHandler = instance.appContext.config.errorHandler
  121. if (appErrorHandler) {
  122. callWithErrorHandling(
  123. appErrorHandler,
  124. null,
  125. ErrorCodes.APP_ERROR_HANDLER,
  126. [err, exposedInstance, errorInfo]
  127. )
  128. return
  129. }
  130. }
  131. logError(err, type, contextVNode, throwInDev)
  132. }
  133. function logError(
  134. err: unknown,
  135. type: ErrorTypes,
  136. contextVNode: VNode | null,
  137. throwInDev = true
  138. ) {
  139. if (__DEV__) {
  140. const info = ErrorTypeStrings[type]
  141. if (contextVNode) {
  142. pushWarningContext(contextVNode)
  143. }
  144. warn(`Unhandled error${info ? ` during execution of ${info}` : ``}`)
  145. if (contextVNode) {
  146. popWarningContext()
  147. }
  148. // crash in dev by default so it's more noticeable
  149. if (throwInDev) {
  150. throw err
  151. } else if (!__TEST__) {
  152. console.error(err)
  153. }
  154. } else {
  155. // recover in prod to reduce the impact on end-user
  156. console.error(err)
  157. }
  158. }