import Vue from "vue";

Vue.config.errorHandler = error => {
  // Vue.prototype.$httpNoAuth.post(
  //   "/api/ecs_core/log/studentClient/" + "debug/007001",
  //   error.stack.replace(/\n/g, '||||'),
  //   {
  //     headers: {
  //       "Content-Type": "text/plain"
  //     }
  //   }
  // );
  window._hmt.push(["_trackEvent", "Vue组件错误"]);
  throw error;
};

// window.onerror = function(message, source, lineno, colno, error) {
//   console.log(message);
//   window._hmt.push([
//     "_trackEvent",
//     "全局JS错误",
//     message,
//     `source: ${source}\nlineno: ${lineno}\ncolno: ${colno}\nerror: ${
//       error.stack
//     }`
//   ]);
// };

window.addEventListener("error", function(event) {
  // Vue.prototype.$httpNoAuth.post(
  //   "/api/ecs_core/log/studentClient/" + "debug/007002",
  //  `message: ${event.message}\nsource: ${event.filename}\nlineno: ${event.lineno}\ncolno: ${
  //   event.colno
  // }\nerror: ${event.error.stack}`.replace(/\n/g, '||||'),
  //   {
  //     headers: {
  //       "Content-Type": "text/plain"
  //     }
  //   }
  // );
  window._hmt.push([
    "_trackEvent",
    "全局JS错误:" + window.location.pathname.replace(/\d+/g, ""),
    event.message,
    event.error.stack.replace(/\n/g, "||||"),
  ]);
});

window.addEventListener("unhandledrejection", function(event) {
  // let detail = "";
  // if (event.reason.config) {
  //   detail += "url:" + event.reason.config.url + "\n";
  //   detail += "data: " + event.reason.config.data + "\n";
  //   if (event.reason.request && event.reason.request.response) {
  //     detail += "response: " + event.reason.request.response + "\n";
  //   }
  // }
  // Vue.prototype.$httpNoAuth.post(
  //   "/api/ecs_core/log/studentClient/" + "debug/007003",
  //  `reason: ${event.reason}\ndetail: ${detail}`.replace(/\n/g, '||||'),
  //   {
  //     headers: {
  //       "Content-Type": "text/plain"
  //     }
  //   }
  // );
  if (
    event.reason &&
    event.reason.message &&
    (event.reason.message.includes("Box.constructor") ||
      event.reason.message.includes("Error: toNetInput"))
  ) {
    window._hmt.push(["_trackEvent", "全局Promise未处理错误", event.reason]);
  } else {
    window._hmt.push([
      "_trackEvent",
      "全局Promise未处理错误",
      window.location.pathname.replace(/\d+/g, ""),
      event.reason,
    ]);
  }
});

window.addEventListener("rejectionhandled", function(event) {
  console.log("rejectionhandled"); // 似乎并不触发
  // let detail = "";
  // if (event.reason.config) {
  //   detail += "url:" + event.reason.config.url + "\n";
  //   detail += "data: " + event.reason.config.data + "\n";
  //   if (event.reason.request && event.reason.request.response) {
  //     detail += "response: " + event.reason.request.response + "\n";
  //   }
  // }
  // Vue.prototype.$httpNoAuth.post(
  //   "/api/ecs_core/log/studentClient/" + "debug/007004",
  //  `reason: ${event.reason}\ndetail: ${detail}`.replace(/\n/g, '||||'),
  //   {
  //     headers: {
  //       "Content-Type": "text/plain"
  //     }
  //   }
  // );
  window._hmt.push(["_trackEvent", "全局Promise已处理错误", event.reason]);
});
