errorReporting.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. const os_1 = __importDefault(require("os"));
  7. const https_1 = __importDefault(require("https"));
  8. const crypto_1 = __importDefault(require("crypto"));
  9. const SERVER_HOST = 'uacer.dcloud.net.cn';
  10. const SERVER_PATH = '/http/error-report-x';
  11. const EXCLUDE_ERROR_LIST = [
  12. 'uni-app-compiler',
  13. 'Error: ENOENT: no such file or directory',
  14. ];
  15. function getMacHash() {
  16. let mac = '';
  17. const network = os_1.default.networkInterfaces();
  18. for (const key in network) {
  19. const array = network[key];
  20. for (let i = 0; i < array.length; i++) {
  21. const item = array[i];
  22. if (!item.family || (item.mac && item.mac === '00:00:00:00:00:00')) {
  23. continue;
  24. }
  25. if (
  26. // Node < v18
  27. typeof item.family === 'string' &&
  28. (item.family === 'IPv4' || item.family === 'IPv6')) {
  29. mac = item.mac;
  30. break;
  31. }
  32. else if (
  33. // Node >= v18
  34. typeof item.family === 'number' &&
  35. (item.family === 4 || item.family === 6)) {
  36. mac = item.mac;
  37. break;
  38. }
  39. }
  40. }
  41. return crypto_1.default.createHash('md5').update(mac).digest('hex');
  42. }
  43. const CacheList = [];
  44. function shouldReport(err = '') {
  45. try {
  46. const errMsg = err.toString();
  47. const errorIndex = EXCLUDE_ERROR_LIST.findIndex((item) => errMsg.includes(item));
  48. if (errorIndex >= 0) {
  49. return false;
  50. }
  51. // 目前简单的上报逻辑为:错误信息中包含@dcloudio包名
  52. if (errMsg.includes('@dcloudio') &&
  53. !errMsg.includes('Errors compiling template')) {
  54. return true;
  55. }
  56. }
  57. catch (e) { }
  58. return false;
  59. }
  60. function report(type, err) {
  61. if (!shouldReport(err)) {
  62. return;
  63. }
  64. if (typeof err === 'object') {
  65. try {
  66. err = err.toString();
  67. }
  68. catch (e) { }
  69. }
  70. const UNI_INPUT_DIR_REG = new RegExp(process.env.UNI_INPUT_DIR, 'ig');
  71. const UNI_CLI_CONTEXT_REG = new RegExp(process.env.UNI_CLI_CONTEXT, 'ig');
  72. err = err.replace(UNI_INPUT_DIR_REG, 'UNI_INPUT_DIR');
  73. err = err.replace(UNI_CLI_CONTEXT_REG, 'UNI_CLI_CONTEXT');
  74. const data = JSON.stringify({
  75. di: getMacHash(),
  76. np: process.platform,
  77. nv: process.version,
  78. cp: process.env.UNI_PLATFORM,
  79. cv: process.env.UNI_COMPILER_VERSION,
  80. hx: process.env.UNI_COMPILER_VERSION_TYPE,
  81. et: type,
  82. em: err,
  83. });
  84. const dataHash = crypto_1.default.createHash('md5').update(data).digest('hex');
  85. if (CacheList.includes(dataHash)) {
  86. return;
  87. }
  88. CacheList.push(dataHash);
  89. setTimeout(() => {
  90. const req = https_1.default.request({
  91. hostname: SERVER_HOST,
  92. port: 443,
  93. path: SERVER_PATH,
  94. method: 'POST',
  95. headers: {
  96. 'Content-Type': 'application/json',
  97. 'Content-Length': data.length,
  98. },
  99. });
  100. req.write(data);
  101. req.end();
  102. }, 10);
  103. }
  104. // @ts-ignore
  105. global.__error_reporting__ = report;
  106. process
  107. .on('unhandledRejection', (reason, p) => {
  108. // @ts-ignore
  109. global.__error_reporting__('unhandledRejection', reason);
  110. console.log(reason);
  111. })
  112. .on('uncaughtException', (err) => {
  113. // @ts-ignore
  114. global.__error_reporting__('uncaughtException', err.stack);
  115. console.log(err);
  116. });