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