sobey-monitor-sdk 1.1.3 → 1.1.4
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.
- package/dist/index.cjs.js +65 -44
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.esm.js +65 -44
- package/dist/index.esm.js.map +1 -1
- package/dist/index.umd.js +65 -44
- package/dist/index.umd.js.map +1 -1
- package/package.json +1 -1
package/dist/index.umd.js
CHANGED
|
@@ -553,11 +553,14 @@
|
|
|
553
553
|
* 安装 JS 错误监听
|
|
554
554
|
*/
|
|
555
555
|
function installJsErrorHandler() {
|
|
556
|
-
const cfg = config.get();
|
|
557
|
-
if (!cfg.error?.enabled || !cfg.error?.jsError) {
|
|
558
|
-
return;
|
|
559
|
-
}
|
|
560
556
|
window.addEventListener('error', (event) => {
|
|
557
|
+
// 延迟检查配置(SDK 可能还在加载配置)
|
|
558
|
+
if (config.isInitialized()) {
|
|
559
|
+
const cfg = config.get();
|
|
560
|
+
if (!cfg.error?.enabled || !cfg.error?.jsError) {
|
|
561
|
+
return;
|
|
562
|
+
}
|
|
563
|
+
}
|
|
561
564
|
// 过滤资源加载错误(由 resourceError 处理)
|
|
562
565
|
if (event.target !== window) {
|
|
563
566
|
return;
|
|
@@ -582,9 +585,6 @@
|
|
|
582
585
|
});
|
|
583
586
|
reporter.reportError(errorData);
|
|
584
587
|
}, true);
|
|
585
|
-
if (cfg.debug) {
|
|
586
|
-
console.log('[Monitor] JS error handler installed');
|
|
587
|
-
}
|
|
588
588
|
}
|
|
589
589
|
|
|
590
590
|
/**
|
|
@@ -594,11 +594,14 @@
|
|
|
594
594
|
* 安装 Promise 错误监听
|
|
595
595
|
*/
|
|
596
596
|
function installPromiseErrorHandler() {
|
|
597
|
-
const cfg = config.get();
|
|
598
|
-
if (!cfg.error?.enabled || !cfg.error?.promiseError) {
|
|
599
|
-
return;
|
|
600
|
-
}
|
|
601
597
|
window.addEventListener('unhandledrejection', (event) => {
|
|
598
|
+
// 延迟检查配置(SDK 可能还在加载配置)
|
|
599
|
+
if (config.isInitialized()) {
|
|
600
|
+
const cfg = config.get();
|
|
601
|
+
if (!cfg.error?.enabled || !cfg.error?.promiseError) {
|
|
602
|
+
return;
|
|
603
|
+
}
|
|
604
|
+
}
|
|
602
605
|
let message = 'Unhandled Promise rejection';
|
|
603
606
|
let stack;
|
|
604
607
|
const reason = event.reason;
|
|
@@ -627,9 +630,6 @@
|
|
|
627
630
|
});
|
|
628
631
|
reporter.reportError(errorData);
|
|
629
632
|
});
|
|
630
|
-
if (cfg.debug) {
|
|
631
|
-
console.log('[Monitor] Promise error handler installed');
|
|
632
|
-
}
|
|
633
633
|
}
|
|
634
634
|
|
|
635
635
|
/**
|
|
@@ -639,11 +639,14 @@
|
|
|
639
639
|
* 安装资源错误监听
|
|
640
640
|
*/
|
|
641
641
|
function installResourceErrorHandler() {
|
|
642
|
-
const cfg = config.get();
|
|
643
|
-
if (!cfg.error?.enabled || !cfg.error?.resourceError) {
|
|
644
|
-
return;
|
|
645
|
-
}
|
|
646
642
|
window.addEventListener('error', (event) => {
|
|
643
|
+
// 延迟检查配置(SDK 可能还在加载配置)
|
|
644
|
+
if (config.isInitialized()) {
|
|
645
|
+
const cfg = config.get();
|
|
646
|
+
if (!cfg.error?.enabled || !cfg.error?.resourceError) {
|
|
647
|
+
return;
|
|
648
|
+
}
|
|
649
|
+
}
|
|
647
650
|
const target = event.target;
|
|
648
651
|
// 只处理资源加载错误(target 不是 window)
|
|
649
652
|
if (!target || target === window || !(target instanceof HTMLElement)) {
|
|
@@ -677,9 +680,6 @@
|
|
|
677
680
|
});
|
|
678
681
|
reporter.reportError(errorData);
|
|
679
682
|
}, true); // 使用捕获阶段
|
|
680
|
-
if (cfg.debug) {
|
|
681
|
-
console.log('[Monitor] Resource error handler installed');
|
|
682
|
-
}
|
|
683
683
|
}
|
|
684
684
|
|
|
685
685
|
/**
|
|
@@ -694,28 +694,32 @@
|
|
|
694
694
|
* 安装 HTTP 错误拦截
|
|
695
695
|
*/
|
|
696
696
|
function installHttpErrorHandler() {
|
|
697
|
-
const cfg = config.get();
|
|
698
|
-
if (!cfg.error?.enabled || !cfg.error?.httpError) {
|
|
699
|
-
return;
|
|
700
|
-
}
|
|
701
697
|
interceptXHR();
|
|
702
698
|
interceptFetch();
|
|
703
|
-
if (cfg.debug) {
|
|
704
|
-
console.log('[Monitor] HTTP error handler installed');
|
|
705
|
-
}
|
|
706
699
|
}
|
|
707
|
-
/**
|
|
708
|
-
* 拦截 XMLHttpRequest
|
|
709
|
-
*/
|
|
710
700
|
/**
|
|
711
701
|
* 检查是否是 SDK 自身的请求
|
|
712
702
|
*/
|
|
713
703
|
function isSdkRequest(url) {
|
|
704
|
+
if (!config.isInitialized())
|
|
705
|
+
return false;
|
|
714
706
|
const cfg = config.get();
|
|
715
707
|
if (!cfg.dsn)
|
|
716
708
|
return false;
|
|
717
709
|
return url.includes(cfg.dsn);
|
|
718
710
|
}
|
|
711
|
+
/**
|
|
712
|
+
* 检查 HTTP 错误监控是否启用
|
|
713
|
+
*/
|
|
714
|
+
function isHttpErrorEnabled() {
|
|
715
|
+
if (!config.isInitialized())
|
|
716
|
+
return true; // 配置未就绪时默认启用,数据会被缓存
|
|
717
|
+
const cfg = config.get();
|
|
718
|
+
return cfg.error?.enabled !== false && cfg.error?.httpError !== false;
|
|
719
|
+
}
|
|
720
|
+
/**
|
|
721
|
+
* 拦截 XMLHttpRequest
|
|
722
|
+
*/
|
|
719
723
|
function interceptXHR() {
|
|
720
724
|
XMLHttpRequest.prototype.open = function (method, url, async = true, username, password) {
|
|
721
725
|
const urlStr = url.toString();
|
|
@@ -745,11 +749,16 @@
|
|
|
745
749
|
this.addEventListener('loadend', function () {
|
|
746
750
|
if (!monitorData)
|
|
747
751
|
return;
|
|
752
|
+
if (!isHttpErrorEnabled())
|
|
753
|
+
return;
|
|
748
754
|
const duration = Date.now() - monitorData.startTime;
|
|
749
755
|
const status = this.status;
|
|
750
756
|
// 根据配置决定是否添加到面包屑
|
|
751
|
-
|
|
752
|
-
|
|
757
|
+
let recordMode = 'error';
|
|
758
|
+
if (config.isInitialized()) {
|
|
759
|
+
const cfg = config.get();
|
|
760
|
+
recordMode = cfg.behavior?.recordRequestBreadcrumb || 'error';
|
|
761
|
+
}
|
|
753
762
|
// 重定向(3xx)始终记录,不受配置控制
|
|
754
763
|
const isRedirect = status >= 300 && status < 400;
|
|
755
764
|
// 其他异常情况(0-网络错误、4xx-客户端错误、5xx-服务端错误)根据配置控制
|
|
@@ -796,11 +805,17 @@
|
|
|
796
805
|
const requestBody = typeof init?.body === 'string' ? init.body : undefined;
|
|
797
806
|
try {
|
|
798
807
|
const response = await originalFetch.call(window, input, init);
|
|
808
|
+
if (!isHttpErrorEnabled()) {
|
|
809
|
+
return response;
|
|
810
|
+
}
|
|
799
811
|
const duration = Date.now() - startTime;
|
|
800
812
|
const status = response.status;
|
|
801
813
|
// 根据配置决定是否添加到面包屑
|
|
802
|
-
|
|
803
|
-
|
|
814
|
+
let recordMode = 'error';
|
|
815
|
+
if (config.isInitialized()) {
|
|
816
|
+
const cfg = config.get();
|
|
817
|
+
recordMode = cfg.behavior?.recordRequestBreadcrumb || 'error';
|
|
818
|
+
}
|
|
804
819
|
// 重定向(3xx)始终记录,不受配置控制
|
|
805
820
|
const isRedirect = status >= 300 && status < 400;
|
|
806
821
|
// 其他异常情况根据配置控制
|
|
@@ -839,10 +854,16 @@
|
|
|
839
854
|
return response;
|
|
840
855
|
}
|
|
841
856
|
catch (error) {
|
|
857
|
+
if (!isHttpErrorEnabled()) {
|
|
858
|
+
throw error;
|
|
859
|
+
}
|
|
842
860
|
const duration = Date.now() - startTime;
|
|
843
861
|
// 网络错误时根据配置决定是否添加到面包屑
|
|
844
|
-
|
|
845
|
-
|
|
862
|
+
let recordMode = 'error';
|
|
863
|
+
if (config.isInitialized()) {
|
|
864
|
+
const cfg = config.get();
|
|
865
|
+
recordMode = cfg.behavior?.recordRequestBreadcrumb || 'error';
|
|
866
|
+
}
|
|
846
867
|
// 网络错误属于 error,当模式为 'all' 或 'error' 时都记录
|
|
847
868
|
if (recordMode !== 'none') {
|
|
848
869
|
context.addBreadcrumb({
|
|
@@ -898,10 +919,6 @@
|
|
|
898
919
|
* 安装白屏检测
|
|
899
920
|
*/
|
|
900
921
|
function installWhiteScreenDetector() {
|
|
901
|
-
const cfg = config.get();
|
|
902
|
-
if (!cfg.error?.enabled) {
|
|
903
|
-
return;
|
|
904
|
-
}
|
|
905
922
|
// 在页面加载完成后检测
|
|
906
923
|
if (document.readyState === 'complete') {
|
|
907
924
|
scheduleDetection();
|
|
@@ -911,9 +928,6 @@
|
|
|
911
928
|
scheduleDetection();
|
|
912
929
|
});
|
|
913
930
|
}
|
|
914
|
-
if (cfg.debug) {
|
|
915
|
-
console.log('[Monitor] White screen detector installed');
|
|
916
|
-
}
|
|
917
931
|
}
|
|
918
932
|
/**
|
|
919
933
|
* 调度检测(延迟执行,给页面渲染时间)
|
|
@@ -921,6 +935,13 @@
|
|
|
921
935
|
function scheduleDetection() {
|
|
922
936
|
// 延迟 1 秒检测
|
|
923
937
|
setTimeout(() => {
|
|
938
|
+
// 检测时才检查配置
|
|
939
|
+
if (config.isInitialized()) {
|
|
940
|
+
const cfg = config.get();
|
|
941
|
+
if (!cfg.error?.enabled) {
|
|
942
|
+
return;
|
|
943
|
+
}
|
|
944
|
+
}
|
|
924
945
|
const isWhiteScreen = detectWhiteScreen();
|
|
925
946
|
if (isWhiteScreen) {
|
|
926
947
|
reportWhiteScreen();
|