zhengyiming
2025-02-10 958b79ed89b9e742540f714a80261d222c0fc09b
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
import { App, nextTick } from 'vue';
import { isString, isArray } from 'lodash';
import settings from '@config/config';
import { useErrorLogStoreHook } from '@/store/modules/errorLog';
 
// 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)) {
    //@ts-ignore
    return needErrorLog.includes(env);
  }
  return false;
}
 
export function initErrorHandler(app: App) {
  if (checkNeed()) {
    app.config.errorHandler = function (err, vm, info) {
      nextTick(() => {
        useErrorLogStoreHook().addErrorLog({
          err,
          vm,
          info,
          url: window.location.href,
        });
        console.error(err, info);
      });
    };
  }
}