e992c4eb532f7fdb6b81e2ec90eebe545dde834cd79bc7d85bc48fa22e295212038aeef93df5d38b345bda5fa5c171d5f3d0d4a8e207be7409948098ecd37d 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* @flow */
  2. import config from '../config'
  3. import { warn } from './debug'
  4. import { inBrowser, inWeex } from './env'
  5. import { isPromise } from 'shared/util'
  6. import { pushTarget, popTarget } from '../observer/dep'
  7. export function handleError (err: Error, vm: any, info: string) {
  8. // Deactivate deps tracking while processing error handler to avoid possible infinite rendering.
  9. // See: https://github.com/vuejs/vuex/issues/1505
  10. pushTarget()
  11. try {
  12. if (vm) {
  13. let cur = vm
  14. while ((cur = cur.$parent)) {
  15. const hooks = cur.$options.errorCaptured
  16. if (hooks) {
  17. for (let i = 0; i < hooks.length; i++) {
  18. try {
  19. const capture = hooks[i].call(cur, err, vm, info) === false
  20. if (capture) return
  21. } catch (e) {
  22. globalHandleError(e, cur, 'errorCaptured hook')
  23. }
  24. }
  25. }
  26. }
  27. }
  28. globalHandleError(err, vm, info)
  29. } finally {
  30. popTarget()
  31. }
  32. }
  33. export function invokeWithErrorHandling (
  34. handler: Function,
  35. context: any,
  36. args: null | any[],
  37. vm: any,
  38. info: string
  39. ) {
  40. let res
  41. try {
  42. res = args ? handler.apply(context, args) : handler.call(context)
  43. if (res && !res._isVue && isPromise(res) && !res._handled) {
  44. res.catch(e => handleError(e, vm, info + ` (Promise/async)`))
  45. // issue #9511
  46. // avoid catch triggering multiple times when nested calls
  47. res._handled = true
  48. }
  49. } catch (e) {
  50. handleError(e, vm, info)
  51. }
  52. return res
  53. }
  54. function globalHandleError (err, vm, info) {
  55. if (config.errorHandler) {
  56. try {
  57. return config.errorHandler.call(null, err, vm, info)
  58. } catch (e) {
  59. // if the user intentionally throws the original error in the handler,
  60. // do not log it twice
  61. if (e !== err) {
  62. logError(e, null, 'config.errorHandler')
  63. }
  64. }
  65. }
  66. logError(err, vm, info)
  67. }
  68. function logError (err, vm, info) {
  69. if (process.env.NODE_ENV !== 'production') {
  70. warn(`Error in ${info}: "${err.toString()}"`, vm)
  71. }
  72. /* istanbul ignore else */
  73. if ((inBrowser || inWeex) && typeof console !== 'undefined') {
  74. console.error(err)
  75. } else {
  76. throw err
  77. }
  78. }