error-log.js 920 B

1234567891011121314151617181920212223242526272829303132333435
  1. import Vue from 'vue'
  2. import store from '@/store'
  3. import { isString, isArray } from '@/utils/validate'
  4. import settings from '@/settings'
  5. // you can set in settings.js
  6. // errorLog:'production' | ['production', 'development']
  7. const { errorLog: needErrorLog } = settings
  8. function checkNeed() {
  9. const env = process.env.NODE_ENV
  10. if (isString(needErrorLog)) {
  11. return env === needErrorLog
  12. }
  13. if (isArray(needErrorLog)) {
  14. return needErrorLog.includes(env)
  15. }
  16. return false
  17. }
  18. if (checkNeed()) {
  19. Vue.config.errorHandler = function(err, vm, info, a) {
  20. // Don't ask me why I use Vue.nextTick, it just a hack.
  21. // detail see https://forum.vuejs.org/t/dispatch-in-vue-config-errorhandler-has-some-problem/23500
  22. Vue.nextTick(() => {
  23. store.dispatch('errorLog/addErrorLog', {
  24. err,
  25. vm,
  26. info,
  27. url: window.location.href
  28. })
  29. console.error(err, info)
  30. })
  31. }
  32. }