@be-link/cls-logger 1.0.1-beta.17 → 1.0.1-beta.19

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
- import type { ClsLoggerInitOptions, ErrorMonitorOptions, EnvType, FlatFields, PerformanceMonitorOptions, PutOptions, QueueItem, ReportLog, RequestMonitorOptions, BehaviorMonitorOptions, DeviceInfoOptions } from './types';
1
+ import type { ClsLoggerInitOptions, ErrorMonitorOptions, EnvType, FlatFields, PerformanceMonitorOptions, PutOptions, QueueItem, ReportLog, ReportOptions, RequestMonitorOptions, BehaviorMonitorOptions, DeviceInfoOptions } from './types';
2
2
  import type { ClsSdkModule } from './clsSdkTypes';
3
3
  /**
4
4
  * CLS Logger 核心基类
@@ -38,6 +38,9 @@ export declare abstract class ClsLoggerCore {
38
38
  protected batchTimerDueAt: number | null;
39
39
  protected initTs: number;
40
40
  protected startupDelayMs: number;
41
+ protected useIdleCallback: boolean;
42
+ protected idleTimeout: number;
43
+ protected visibilityCleanup: (() => void) | null;
41
44
  protected failedCacheKey: string;
42
45
  protected failedCacheMax: number;
43
46
  protected requestMonitorStarted: boolean;
@@ -77,6 +80,22 @@ export declare abstract class ClsLoggerCore {
77
80
  protected detectEnvType(): EnvType;
78
81
  init(options: ClsLoggerInitOptions): void;
79
82
  private getBaseFields;
83
+ /**
84
+ * 设置页面可见性监听
85
+ * - visibilitychange: 页面隐藏时使用 sendBeacon 发送队列
86
+ * - pagehide: 作为移动端 fallback
87
+ */
88
+ private setupVisibilityListener;
89
+ /**
90
+ * 同步发送内存队列(使用 sendBeacon)
91
+ * - 用于页面关闭时确保数据发送
92
+ * - sendBeacon 不可用时降级为缓存到 localStorage
93
+ */
94
+ private flushBatchSync;
95
+ /**
96
+ * 构建 sendBeacon 的 payload
97
+ */
98
+ private buildSendBeaconPayload;
80
99
  private startRequestMonitor;
81
100
  private startErrorMonitor;
82
101
  private startPerformanceMonitor;
@@ -121,10 +140,20 @@ export declare abstract class ClsLoggerCore {
121
140
  * 参考《一、概述》:统一上报入口(内存队列 + 批量发送)
122
141
  */
123
142
  report(log: ReportLog): void;
143
+ /**
144
+ * 调度批量发送
145
+ * - 支持 requestIdleCallback(浏览器空闲时执行)
146
+ * - 降级为 setTimeout
147
+ */
148
+ private scheduleFlush;
149
+ /**
150
+ * 取消已调度的批量发送
151
+ */
152
+ private cancelScheduledFlush;
124
153
  private getDesiredBatchFlushDueAt;
125
- info(message: string | Error, data?: FlatFields): void;
126
- warn(message: string | Error, data?: FlatFields): void;
127
- error(message: string | Error, data?: FlatFields): void;
154
+ info(message: string | Error, data?: FlatFields, options?: ReportOptions): void;
155
+ warn(message: string | Error, data?: FlatFields, options?: ReportOptions): void;
156
+ error(message: string | Error, data?: FlatFields, options?: ReportOptions): void;
128
157
  track(trackType: string, data?: FlatFields): void;
129
158
  /**
130
159
  * 立即发送内存队列
@@ -1 +1 @@
1
- {"version":3,"file":"ClsLoggerCore.d.ts","sourceRoot":"","sources":["../src/ClsLoggerCore.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,oBAAoB,EACpB,mBAAmB,EACnB,OAAO,EACP,UAAU,EACV,yBAAyB,EACzB,UAAU,EACV,SAAS,EACT,SAAS,EACT,qBAAqB,EACrB,sBAAsB,EACtB,iBAAiB,EAClB,MAAM,SAAS,CAAC;AAWjB,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAYlD;;;;;GAKG;AACH,8BAAsB,aAAa;IACjC,SAAS,CAAC,GAAG,EAAE,YAAY,GAAG,IAAI,CAAQ;IAC1C,SAAS,CAAC,UAAU,EAAE,OAAO,CAAC,YAAY,CAAC,GAAG,IAAI,CAAQ;IAC1D,SAAS,CAAC,WAAW,EAAE,OAAO,GAAG,IAAI,CAAQ;IAC7C,SAAS,CAAC,iBAAiB,EAAE,CAAC,MAAM,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC,CAAC,GAAG,IAAI,CA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AAE,MAAM,GAAG,KAAK,EAAE,IAAI,GAAE,UAAe,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,IAAI;IA6BnF,IAAI,CAAC,OAAO,EAAE,MAAM,GAAG,KAAK,EAAE,IAAI,GAAE,UAAe,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,IAAI;IA6BnF,KAAK,CAAC,OAAO,EAAE,MAAM,GAAG,KAAK,EAAE,IAAI,GAAE,UAAe,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,IAAI;IA8BpF,KAAK,CAAC,SAAS,EAAE,MAAM,EAAE,IAAI,GAAE,UAAe,GAAG,IAAI;IASrD;;OAEG;IACG,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAejC,OAAO,CAAC,iBAAiB;YAmBX,cAAc;IAgC5B,OAAO,CAAC,mBAAmB;IAgB3B,OAAO,CAAC,qBAAqB;IAc7B,WAAW,IAAI,IAAI;IAmBnB;;OAEG;IACH,IAAI,CAAC,KAAK,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;KAAE,GAAG,IAAI;CAcjG"}
package/dist/index.esm.js CHANGED
@@ -176,6 +176,9 @@ class ClsLoggerCore {
176
176
  this.batchTimerDueAt = null;
177
177
  this.initTs = 0;
178
178
  this.startupDelayMs = 0;
179
+ this.useIdleCallback = false;
180
+ this.idleTimeout = 3000;
181
+ this.visibilityCleanup = null;
179
182
  // 参考文档:失败缓存 + 重试
180
183
  this.failedCacheKey = 'cls_failed_logs';
181
184
  this.failedCacheMax = 200;
@@ -245,6 +248,8 @@ class ClsLoggerCore {
245
248
  this.batchMaxSize = options.batch?.maxSize ?? this.batchMaxSize;
246
249
  this.batchIntervalMs = options.batch?.intervalMs ?? this.batchIntervalMs;
247
250
  this.startupDelayMs = options.batch?.startupDelayMs ?? this.startupDelayMs;
251
+ this.useIdleCallback = options.batch?.useIdleCallback ?? this.useIdleCallback;
252
+ this.idleTimeout = options.batch?.idleTimeout ?? this.idleTimeout;
248
253
  this.failedCacheKey = options.failedCacheKey ?? this.failedCacheKey;
249
254
  this.failedCacheMax = options.failedCacheMax ?? this.failedCacheMax;
250
255
  // 预热(避免首条日志触发 import/初始化开销)
@@ -254,6 +259,8 @@ class ClsLoggerCore {
254
259
  if (this.enabled) {
255
260
  // 启动时尝试发送失败缓存
256
261
  this.flushFailed();
262
+ // 添加页面可见性监听(确保页面关闭时数据不丢失)
263
+ this.setupVisibilityListener();
257
264
  // 初始化后立即启动请求监听
258
265
  this.startRequestMonitor(options.requestMonitor);
259
266
  // 初始化后立即启动错误监控/性能监控
@@ -290,6 +297,89 @@ class ClsLoggerCore {
290
297
  return auto;
291
298
  return undefined;
292
299
  }
300
+ /**
301
+ * 设置页面可见性监听
302
+ * - visibilitychange: 页面隐藏时使用 sendBeacon 发送队列
303
+ * - pagehide: 作为移动端 fallback
304
+ */
305
+ setupVisibilityListener() {
306
+ if (typeof document === 'undefined' || typeof window === 'undefined')
307
+ return;
308
+ // 避免重复监听
309
+ if (this.visibilityCleanup)
310
+ return;
311
+ const handleVisibilityChange = () => {
312
+ if (document.visibilityState === 'hidden') {
313
+ this.flushBatchSync();
314
+ }
315
+ };
316
+ const handlePageHide = () => {
317
+ this.flushBatchSync();
318
+ };
319
+ document.addEventListener('visibilitychange', handleVisibilityChange);
320
+ window.addEventListener('pagehide', handlePageHide);
321
+ this.visibilityCleanup = () => {
322
+ document.removeEventListener('visibilitychange', handleVisibilityChange);
323
+ window.removeEventListener('pagehide', handlePageHide);
324
+ };
325
+ }
326
+ /**
327
+ * 同步发送内存队列(使用 sendBeacon)
328
+ * - 用于页面关闭时确保数据发送
329
+ * - sendBeacon 不可用时降级为缓存到 localStorage
330
+ */
331
+ flushBatchSync() {
332
+ if (this.memoryQueue.length === 0)
333
+ return;
334
+ // 清除定时器
335
+ if (this.batchTimer) {
336
+ try {
337
+ if (this.useIdleCallback && typeof cancelIdleCallback !== 'undefined') {
338
+ cancelIdleCallback(this.batchTimer);
339
+ }
340
+ else {
341
+ clearTimeout(this.batchTimer);
342
+ }
343
+ }
344
+ catch {
345
+ // ignore
346
+ }
347
+ this.batchTimer = null;
348
+ }
349
+ this.batchTimerDueAt = null;
350
+ const logs = [...this.memoryQueue];
351
+ this.memoryQueue = [];
352
+ // 优先使用 sendBeacon(页面关闭时可靠发送)
353
+ if (typeof navigator !== 'undefined' && typeof navigator.sendBeacon === 'function') {
354
+ try {
355
+ const payload = this.buildSendBeaconPayload(logs);
356
+ const blob = new Blob([payload], { type: 'application/json' });
357
+ const url = `${this.endpoint}/structuredlog?topic_id=${this.topicId}`;
358
+ const success = navigator.sendBeacon(url, blob);
359
+ if (!success) {
360
+ // sendBeacon 返回 false 时,降级缓存
361
+ this.cacheFailedReportLogs(logs);
362
+ }
363
+ }
364
+ catch {
365
+ this.cacheFailedReportLogs(logs);
366
+ }
367
+ }
368
+ else {
369
+ // 不支持 sendBeacon,降级缓存到 localStorage
370
+ this.cacheFailedReportLogs(logs);
371
+ }
372
+ }
373
+ /**
374
+ * 构建 sendBeacon 的 payload
375
+ */
376
+ buildSendBeaconPayload(logs) {
377
+ const logList = logs.map((log) => this.buildReportFields(log));
378
+ return JSON.stringify({
379
+ source: this.source,
380
+ logs: logList,
381
+ });
382
+ }
293
383
  startRequestMonitor(requestMonitor) {
294
384
  if (this.requestMonitorStarted)
295
385
  return;
@@ -544,25 +634,55 @@ class ClsLoggerCore {
544
634
  const desiredDelay = Math.max(0, desiredDueAt - now);
545
635
  if (!this.batchTimer) {
546
636
  this.batchTimerDueAt = desiredDueAt;
547
- this.batchTimer = setTimeout(() => {
548
- void this.flushBatch();
549
- }, desiredDelay);
637
+ this.scheduleFlush(desiredDelay);
550
638
  return;
551
639
  }
552
- // 启动合并窗口内:如果当前 timer 会“更早”触发,则延后到窗口结束,尽量减少多次发送
640
+ // 启动合并窗口内:如果当前 timer 会"更早"触发,则延后到窗口结束,尽量减少多次发送
553
641
  if (this.batchTimerDueAt !== null && this.batchTimerDueAt < desiredDueAt) {
554
- try {
555
- clearTimeout(this.batchTimer);
556
- }
557
- catch {
558
- // ignore
559
- }
642
+ this.cancelScheduledFlush();
560
643
  this.batchTimerDueAt = desiredDueAt;
644
+ this.scheduleFlush(desiredDelay);
645
+ }
646
+ }
647
+ /**
648
+ * 调度批量发送
649
+ * - 支持 requestIdleCallback(浏览器空闲时执行)
650
+ * - 降级为 setTimeout
651
+ */
652
+ scheduleFlush(desiredDelay) {
653
+ if (this.useIdleCallback && typeof requestIdleCallback !== 'undefined') {
654
+ // 使用 requestIdleCallback,设置 timeout 保证最终执行
655
+ const idleId = requestIdleCallback(() => {
656
+ void this.flushBatch();
657
+ }, { timeout: Math.max(desiredDelay, this.idleTimeout) });
658
+ // 存储 idleId 以便清理(类型兼容处理)
659
+ this.batchTimer = idleId;
660
+ }
661
+ else {
561
662
  this.batchTimer = setTimeout(() => {
562
663
  void this.flushBatch();
563
664
  }, desiredDelay);
564
665
  }
565
666
  }
667
+ /**
668
+ * 取消已调度的批量发送
669
+ */
670
+ cancelScheduledFlush() {
671
+ if (!this.batchTimer)
672
+ return;
673
+ try {
674
+ if (this.useIdleCallback && typeof cancelIdleCallback !== 'undefined') {
675
+ cancelIdleCallback(this.batchTimer);
676
+ }
677
+ else {
678
+ clearTimeout(this.batchTimer);
679
+ }
680
+ }
681
+ catch {
682
+ // ignore
683
+ }
684
+ this.batchTimer = null;
685
+ }
566
686
  getDesiredBatchFlushDueAt(nowTs) {
567
687
  const start = this.initTs || nowTs;
568
688
  const startupDelay = Number.isFinite(this.startupDelayMs) ? Math.max(0, this.startupDelayMs) : 0;
@@ -573,7 +693,7 @@ class ClsLoggerCore {
573
693
  }
574
694
  return nowTs + this.batchIntervalMs;
575
695
  }
576
- info(message, data = {}) {
696
+ info(message, data = {}, options) {
577
697
  let msg = '';
578
698
  let extra = {};
579
699
  if (message instanceof Error) {
@@ -589,9 +709,18 @@ class ClsLoggerCore {
589
709
  extra = data;
590
710
  }
591
711
  const payload = normalizeFlatFields({ message: msg, ...extra }, 'info');
592
- this.report({ type: 'info', data: payload, timestamp: Date.now() });
712
+ const log = { type: 'info', data: payload, timestamp: Date.now() };
713
+ // info 默认走批量队列,支持 immediate 选项立即发送
714
+ if (options?.immediate) {
715
+ void this.sendReportLogs([log]).catch(() => {
716
+ this.cacheFailedReportLogs([log]);
717
+ });
718
+ }
719
+ else {
720
+ this.report(log);
721
+ }
593
722
  }
594
- warn(message, data = {}) {
723
+ warn(message, data = {}, options) {
595
724
  let msg = '';
596
725
  let extra = {};
597
726
  if (message instanceof Error) {
@@ -607,9 +736,18 @@ class ClsLoggerCore {
607
736
  extra = data;
608
737
  }
609
738
  const payload = normalizeFlatFields({ message: msg, ...extra }, 'warn');
610
- this.report({ type: 'warn', data: payload, timestamp: Date.now() });
739
+ const log = { type: 'warn', data: payload, timestamp: Date.now() };
740
+ // warn 默认走批量队列,支持 immediate 选项立即发送
741
+ if (options?.immediate) {
742
+ void this.sendReportLogs([log]).catch(() => {
743
+ this.cacheFailedReportLogs([log]);
744
+ });
745
+ }
746
+ else {
747
+ this.report(log);
748
+ }
611
749
  }
612
- error(message, data = {}) {
750
+ error(message, data = {}, options) {
613
751
  let msg = '';
614
752
  let extra = {};
615
753
  if (message instanceof Error) {
@@ -625,7 +763,17 @@ class ClsLoggerCore {
625
763
  extra = data;
626
764
  }
627
765
  const payload = normalizeFlatFields({ message: msg, ...extra }, 'error');
628
- this.report({ type: 'error', data: payload, timestamp: Date.now() });
766
+ const log = { type: 'error', data: payload, timestamp: Date.now() };
767
+ // error 默认即时上报,除非显式指定 immediate: false
768
+ const immediate = options?.immediate ?? true;
769
+ if (immediate) {
770
+ void this.sendReportLogs([log]).catch(() => {
771
+ this.cacheFailedReportLogs([log]);
772
+ });
773
+ }
774
+ else {
775
+ this.report(log);
776
+ }
629
777
  }
630
778
  track(trackType, data = {}) {
631
779
  if (!trackType)
@@ -640,10 +788,7 @@ class ClsLoggerCore {
640
788
  * 立即发送内存队列
641
789
  */
642
790
  async flushBatch() {
643
- if (this.batchTimer) {
644
- clearTimeout(this.batchTimer);
645
- this.batchTimer = null;
646
- }
791
+ this.cancelScheduledFlush();
647
792
  this.batchTimerDueAt = null;
648
793
  if (this.memoryQueue.length === 0)
649
794
  return;
@@ -1288,6 +1433,13 @@ function installRequestMonitor(report, opts = {}) {
1288
1433
  const DEFAULT_MAX_TEXT$1 = 4000;
1289
1434
  const DEFAULT_DEDUPE_WINDOW_MS$1 = 3000;
1290
1435
  const DEFAULT_DEDUPE_MAX_KEYS$1 = 200;
1436
+ /** 默认忽略的无意义错误信息 */
1437
+ const DEFAULT_IGNORE_MESSAGES$1 = [
1438
+ 'Script error.',
1439
+ 'Script error',
1440
+ /^ResizeObserver loop/,
1441
+ 'Permission was denied',
1442
+ ];
1291
1443
  function truncate$3(s, maxLen) {
1292
1444
  if (!s)
1293
1445
  return s;
@@ -1300,6 +1452,17 @@ function sampleHit$3(sampleRate) {
1300
1452
  return false;
1301
1453
  return Math.random() < sampleRate;
1302
1454
  }
1455
+ /** 检查消息是否应该被忽略 */
1456
+ function shouldIgnoreMessage$1(message, ignorePatterns) {
1457
+ if (!message || ignorePatterns.length === 0)
1458
+ return false;
1459
+ return ignorePatterns.some((pattern) => {
1460
+ if (typeof pattern === 'string') {
1461
+ return message === pattern || message.includes(pattern);
1462
+ }
1463
+ return pattern.test(message);
1464
+ });
1465
+ }
1303
1466
  function getPagePath$1() {
1304
1467
  try {
1305
1468
  if (typeof window === 'undefined')
@@ -1395,6 +1558,9 @@ function installBrowserErrorMonitor(report, options) {
1395
1558
  };
1396
1559
  if (event && typeof event === 'object' && 'message' in event) {
1397
1560
  payload.message = truncate$3(String(event.message ?? ''), options.maxTextLength);
1561
+ // 检查是否应该忽略此错误
1562
+ if (shouldIgnoreMessage$1(String(event.message ?? ''), options.ignoreMessages))
1563
+ return;
1398
1564
  payload.filename = truncate$3(String(event.filename ?? ''), 500);
1399
1565
  payload.lineno = typeof event.lineno === 'number' ? event.lineno : undefined;
1400
1566
  payload.colno = typeof event.colno === 'number' ? event.colno : undefined;
@@ -1433,6 +1599,9 @@ function installBrowserErrorMonitor(report, options) {
1433
1599
  return;
1434
1600
  const reason = event?.reason;
1435
1601
  const e = normalizeErrorLike$1(reason, options.maxTextLength);
1602
+ // 检查是否应该忽略此错误
1603
+ if (shouldIgnoreMessage$1(e.message, options.ignoreMessages))
1604
+ return;
1436
1605
  const payload = {
1437
1606
  pagePath: getPagePath$1(),
1438
1607
  source: 'unhandledrejection',
@@ -1462,6 +1631,7 @@ function installWebErrorMonitor(report, opts = {}) {
1462
1631
  maxTextLength: raw.maxTextLength ?? DEFAULT_MAX_TEXT$1,
1463
1632
  dedupeWindowMs: raw.dedupeWindowMs ?? DEFAULT_DEDUPE_WINDOW_MS$1,
1464
1633
  dedupeMaxKeys: raw.dedupeMaxKeys ?? DEFAULT_DEDUPE_MAX_KEYS$1,
1634
+ ignoreMessages: raw.ignoreMessages ?? DEFAULT_IGNORE_MESSAGES$1,
1465
1635
  };
1466
1636
  installBrowserErrorMonitor(report, options);
1467
1637
  }
@@ -1469,6 +1639,13 @@ function installWebErrorMonitor(report, opts = {}) {
1469
1639
  const DEFAULT_MAX_TEXT = 4000;
1470
1640
  const DEFAULT_DEDUPE_WINDOW_MS = 3000;
1471
1641
  const DEFAULT_DEDUPE_MAX_KEYS = 200;
1642
+ /** 默认忽略的无意义错误信息 */
1643
+ const DEFAULT_IGNORE_MESSAGES = [
1644
+ 'Script error.',
1645
+ 'Script error',
1646
+ /^ResizeObserver loop/,
1647
+ 'Permission was denied',
1648
+ ];
1472
1649
  function truncate$2(s, maxLen) {
1473
1650
  if (!s)
1474
1651
  return s;
@@ -1481,6 +1658,17 @@ function sampleHit$2(sampleRate) {
1481
1658
  return false;
1482
1659
  return Math.random() < sampleRate;
1483
1660
  }
1661
+ /** 检查消息是否应该被忽略 */
1662
+ function shouldIgnoreMessage(message, ignorePatterns) {
1663
+ if (!message || ignorePatterns.length === 0)
1664
+ return false;
1665
+ return ignorePatterns.some((pattern) => {
1666
+ if (typeof pattern === 'string') {
1667
+ return message === pattern || message.includes(pattern);
1668
+ }
1669
+ return pattern.test(message);
1670
+ });
1671
+ }
1484
1672
  function getMpPagePath() {
1485
1673
  try {
1486
1674
  const pages = globalThis.getCurrentPages?.();
@@ -1575,6 +1763,9 @@ function installMiniProgramErrorMonitor(report, options) {
1575
1763
  if (!sampleHit$2(options.sampleRate))
1576
1764
  return;
1577
1765
  const e = normalizeErrorLike(msg, options.maxTextLength);
1766
+ // 检查是否应该忽略此错误
1767
+ if (shouldIgnoreMessage(e.message, options.ignoreMessages))
1768
+ return;
1578
1769
  const payload = {
1579
1770
  pagePath: getMpPagePath(),
1580
1771
  source: 'wx.onError',
@@ -1602,6 +1793,9 @@ function installMiniProgramErrorMonitor(report, options) {
1602
1793
  if (!sampleHit$2(options.sampleRate))
1603
1794
  return;
1604
1795
  const e = normalizeErrorLike(res?.reason, options.maxTextLength);
1796
+ // 检查是否应该忽略此错误
1797
+ if (shouldIgnoreMessage(e.message, options.ignoreMessages))
1798
+ return;
1605
1799
  const payload = {
1606
1800
  pagePath: getMpPagePath(),
1607
1801
  source: 'wx.onUnhandledRejection',
@@ -1633,15 +1827,18 @@ function installMiniProgramErrorMonitor(report, options) {
1633
1827
  try {
1634
1828
  if (sampleHit$2(options.sampleRate)) {
1635
1829
  const e = normalizeErrorLike(args?.[0], options.maxTextLength);
1636
- const payload = {
1637
- pagePath: getMpPagePath(),
1638
- source: 'App.onError',
1639
- message: e.message,
1640
- errorName: e.name,
1641
- stack: e.stack,
1642
- };
1643
- if (shouldReport(buildErrorKey(options.reportType, payload)))
1644
- report(options.reportType, payload);
1830
+ // 检查是否应该忽略此错误
1831
+ if (!shouldIgnoreMessage(e.message, options.ignoreMessages)) {
1832
+ const payload = {
1833
+ pagePath: getMpPagePath(),
1834
+ source: 'App.onError',
1835
+ message: e.message,
1836
+ errorName: e.name,
1837
+ stack: e.stack,
1838
+ };
1839
+ if (shouldReport(buildErrorKey(options.reportType, payload)))
1840
+ report(options.reportType, payload);
1841
+ }
1645
1842
  }
1646
1843
  }
1647
1844
  catch {
@@ -1657,15 +1854,18 @@ function installMiniProgramErrorMonitor(report, options) {
1657
1854
  if (sampleHit$2(options.sampleRate)) {
1658
1855
  const reason = args?.[0]?.reason ?? args?.[0];
1659
1856
  const e = normalizeErrorLike(reason, options.maxTextLength);
1660
- const payload = {
1661
- pagePath: getMpPagePath(),
1662
- source: 'App.onUnhandledRejection',
1663
- message: e.message,
1664
- errorName: e.name,
1665
- stack: e.stack,
1666
- };
1667
- if (shouldReport(buildErrorKey(options.reportType, payload)))
1668
- report(options.reportType, payload);
1857
+ // 检查是否应该忽略此错误
1858
+ if (!shouldIgnoreMessage(e.message, options.ignoreMessages)) {
1859
+ const payload = {
1860
+ pagePath: getMpPagePath(),
1861
+ source: 'App.onUnhandledRejection',
1862
+ message: e.message,
1863
+ errorName: e.name,
1864
+ stack: e.stack,
1865
+ };
1866
+ if (shouldReport(buildErrorKey(options.reportType, payload)))
1867
+ report(options.reportType, payload);
1868
+ }
1669
1869
  }
1670
1870
  }
1671
1871
  catch {
@@ -1696,6 +1896,7 @@ function installMiniErrorMonitor(report, opts = {}) {
1696
1896
  maxTextLength: raw.maxTextLength ?? DEFAULT_MAX_TEXT,
1697
1897
  dedupeWindowMs: raw.dedupeWindowMs ?? DEFAULT_DEDUPE_WINDOW_MS,
1698
1898
  dedupeMaxKeys: raw.dedupeMaxKeys ?? DEFAULT_DEDUPE_MAX_KEYS,
1899
+ ignoreMessages: raw.ignoreMessages ?? DEFAULT_IGNORE_MESSAGES,
1699
1900
  };
1700
1901
  installMiniProgramErrorMonitor(report, options);
1701
1902
  }