UNPKG

whitch-frontend-monitor

Version:

前端监控系统 - 一个轻量级的前端监控SDK

119 lines 4.71 kB
"use strict"; /** * 错误监控模块 */ Object.defineProperty(exports, "__esModule", { value: true }); exports.ErrorMonitor = void 0; var reporter_1 = require("../reporter"); var ErrorMonitor = /** @class */ (function () { function ErrorMonitor() { } /** * 初始化错误监控 */ ErrorMonitor.init = function () { var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m; // 获取已初始化的Reporter实例 var reportConfig = { reportUrl: ((_b = (_a = window.TraeMonitor) === null || _a === void 0 ? void 0 : _a.config) === null || _b === void 0 ? void 0 : _b.reportUrl) || '', delay: ((_d = (_c = window.TraeMonitor) === null || _c === void 0 ? void 0 : _c.config) === null || _d === void 0 ? void 0 : _d.delay) || 1000, maxRetry: ((_f = (_e = window.TraeMonitor) === null || _e === void 0 ? void 0 : _e.config) === null || _f === void 0 ? void 0 : _f.maxRetry) || 3, sampleRate: ((_h = (_g = window.TraeMonitor) === null || _g === void 0 ? void 0 : _g.config) === null || _h === void 0 ? void 0 : _h.sampleRate) || 1, ignoreUrls: ((_k = (_j = window.TraeMonitor) === null || _j === void 0 ? void 0 : _j.config) === null || _k === void 0 ? void 0 : _k.ignoreUrls) || [], compression: ((_m = (_l = window.TraeMonitor) === null || _l === void 0 ? void 0 : _l.config) === null || _m === void 0 ? void 0 : _m.compression) || false }; if (!reportConfig.reportUrl) { console.warn('Reporter URL is not configured'); return; } this.reporter = reporter_1.Reporter.getInstance(reportConfig); this.initJsErrorListener(); this.initPromiseErrorListener(); this.initResourceErrorListener(); }; /** * 监听JS运行时错误 */ ErrorMonitor.initJsErrorListener = function () { var _this = this; window.onerror = function (message, filename, lineno, colno, error) { var errorInfo = { type: 'javascript', message: message.toString(), filename: filename, position: "".concat(lineno, ":").concat(colno), stack: error === null || error === void 0 ? void 0 : error.stack, timestamp: Date.now() }; _this.errorList.push(errorInfo); // 添加到上报队列 _this.reporter.addToQueue({ type: 'error', data: errorInfo, timestamp: Date.now() }); }; }; /** * 监听Promise未处理的异常 */ ErrorMonitor.initPromiseErrorListener = function () { var _this = this; window.addEventListener('unhandledrejection', function (event) { var _a, _b; var errorInfo = { type: 'promise', message: ((_a = event.reason) === null || _a === void 0 ? void 0 : _a.message) || event.reason || 'Promise Rejection', stack: (_b = event.reason) === null || _b === void 0 ? void 0 : _b.stack, timestamp: Date.now() }; _this.errorList.push(errorInfo); // 添加到上报队列 _this.reporter.addToQueue({ type: 'error', data: errorInfo, timestamp: Date.now() }); }); }; /** * 监听资源加载错误 */ ErrorMonitor.initResourceErrorListener = function () { var _this = this; window.addEventListener('error', function (event) { if (event.target && event.target.tagName) { var target = event.target; var errorInfo = { type: 'resource', message: "Resource load failed: ".concat(target.tagName.toLowerCase()), filename: target.src || target.href, timestamp: Date.now() }; _this.errorList.push(errorInfo); // 添加到上报队列 _this.reporter.addToQueue({ type: 'error', data: errorInfo, timestamp: Date.now() }); } }, true); }; /** * 获取收集到的错误信息 */ ErrorMonitor.getErrors = function () { return this.errorList; }; /** * 清空错误信息 */ ErrorMonitor.clearErrors = function () { this.errorList = []; }; ErrorMonitor.errorList = []; return ErrorMonitor; }()); exports.ErrorMonitor = ErrorMonitor; //# sourceMappingURL=error.js.map