浏览代码

暂时采用百度统计作为错误上报

Michael Wang 4 年之前
父节点
当前提交
f018c4a57f
共有 3 个文件被更改,包括 37 次插入0 次删除
  1. 2 0
      src/main.js
  2. 24 0
      src/utils/monitors.js
  3. 11 0
      src/utils/utils.js

+ 2 - 0
src/main.js

@@ -15,6 +15,8 @@ import "./plugins/element.js";
 import "./plugins/vueAwesome";
 import "./plugins/helpers";
 
+import "./utils/monitors";
+
 // styles begin
 // bootstrap 133KB non-zip
 import "./styles/bootstrap.scss";

+ 24 - 0
src/utils/monitors.js

@@ -0,0 +1,24 @@
+import Vue from "vue";
+import { errorLog } from "@/utils/utils";
+
+Vue.config.errorHandler = (error) => {
+  errorLog(error.message, {
+    stack: error.stack,
+    code: "Vue组件错误",
+  });
+  throw error;
+};
+
+window.addEventListener("error", function (event) {
+  errorLog(event.message, {
+    stack: event.error.stack.replace(/\n/g, "||||"),
+    code: "全局JS错误",
+  });
+});
+
+window.addEventListener("unhandledrejection", function (event) {
+  errorLog(event?.reason?.message, {
+    stack: event.error.stack.replace(/\n/g, "||||"),
+    code: "全局JS错误",
+  });
+});

+ 11 - 0
src/utils/utils.js

@@ -11,3 +11,14 @@ export function formatEmptyToNull(obj) {
   });
   return obj;
 }
+
+// 错误上报:本地打印,百度统计,阿里云日志
+export function errorLog(message, { stack = "", code = "" }) {
+  console.error({ message, stack, code });
+  window._hmt.push([
+    "_trackEvent",
+    "message: " + message,
+    stack && "stack: " + stack,
+    code && "code: " + code,
+  ]);
+}