monitors.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import Vue from "vue";
  2. import { createLog } from "@/utils/logger";
  3. Vue.config.errorHandler = (error) => {
  4. window._hmt.push(["_trackEvent", "Vue组件错误"]);
  5. createLog({
  6. action: "Vue组件错误",
  7. path: window.location,
  8. errorJSON: JSON.stringify(error, (key, value) =>
  9. key === "token" ? "" : value
  10. ),
  11. errorName: error.name,
  12. errorMessage: error.message,
  13. errorStack: error.stack,
  14. });
  15. throw error;
  16. };
  17. window.addEventListener("error", function (event) {
  18. window._hmt.push([
  19. "_trackEvent",
  20. "全局JS错误:" +
  21. window.location.pathname.replace(/=\w*/g, "=").replace(/\d+/g, "{id}"),
  22. event.message,
  23. "detail see ali",
  24. ]);
  25. createLog({
  26. action: "全局JS错误",
  27. page: window.location,
  28. message: event.message,
  29. errorJSON: JSON.stringify(event.error, (key, value) =>
  30. key === "token" ? "" : value
  31. ),
  32. errorName: event.error.name,
  33. errorMessage: event.error.message,
  34. errorStack: event.error.stack,
  35. errorFileName: event.error.filename,
  36. errorLineNo: event.error.lineno,
  37. errorColNo: event.error.colno,
  38. });
  39. });
  40. window.addEventListener("unhandledrejection", function (event) {
  41. // 此错误由上传阿里云日志触发,会被重复好几次
  42. // 改为fetch,阿里云日志的错误不应该触发到这里
  43. console.log(
  44. "unhandledrejection event",
  45. event,
  46. event.reason,
  47. JSON.stringify(event.reason)
  48. );
  49. // 会造成死循环,logger.log 在网络异常的情况下,会有unhandledrejection
  50. createLog({
  51. action: "unhandledrejection错误",
  52. page: window.location.pathname,
  53. reason: event.reason,
  54. reasonJson: JSON.stringify(event.reason),
  55. });
  56. if (
  57. event.reason?.message?.includes("Box.constructor") ||
  58. event.reason?.message?.includes("Error: toNetInput")
  59. ) {
  60. window._hmt.push([
  61. "_trackEvent",
  62. "全局Promise未处理错误",
  63. event.reason.message.replace(/=\w*/g, "=").replace(/\d+/g, "{id}"),
  64. ]);
  65. } else {
  66. window._hmt.push([
  67. "_trackEvent",
  68. "全局Promise未处理错误",
  69. window.location.pathname.replace(/=\w*/g, "=").replace(/\d+/g, "{id}"),
  70. event.reason.message.replace(/=\w*/g, "=").replace(/\d+/g, "{id}"),
  71. ]);
  72. }
  73. });
  74. window.addEventListener("rejectionhandled", function (event) {
  75. console.log("rejectionhandled"); // 似乎并不触发
  76. window._hmt.push(["_trackEvent", "全局Promise已处理错误", event.reason]);
  77. });