errorHandling.ts 4.9 KB

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