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.esm.js
CHANGED
|
@@ -547,11 +547,14 @@ const reporter = new Reporter();
|
|
|
547
547
|
* 安装 JS 错误监听
|
|
548
548
|
*/
|
|
549
549
|
function installJsErrorHandler() {
|
|
550
|
-
const cfg = config.get();
|
|
551
|
-
if (!cfg.error?.enabled || !cfg.error?.jsError) {
|
|
552
|
-
return;
|
|
553
|
-
}
|
|
554
550
|
window.addEventListener('error', (event) => {
|
|
551
|
+
// 延迟检查配置(SDK 可能还在加载配置)
|
|
552
|
+
if (config.isInitialized()) {
|
|
553
|
+
const cfg = config.get();
|
|
554
|
+
if (!cfg.error?.enabled || !cfg.error?.jsError) {
|
|
555
|
+
return;
|
|
556
|
+
}
|
|
557
|
+
}
|
|
555
558
|
// 过滤资源加载错误(由 resourceError 处理)
|
|
556
559
|
if (event.target !== window) {
|
|
557
560
|
return;
|
|
@@ -576,9 +579,6 @@ function installJsErrorHandler() {
|
|
|
576
579
|
});
|
|
577
580
|
reporter.reportError(errorData);
|
|
578
581
|
}, true);
|
|
579
|
-
if (cfg.debug) {
|
|
580
|
-
console.log('[Monitor] JS error handler installed');
|
|
581
|
-
}
|
|
582
582
|
}
|
|
583
583
|
|
|
584
584
|
/**
|
|
@@ -588,11 +588,14 @@ function installJsErrorHandler() {
|
|
|
588
588
|
* 安装 Promise 错误监听
|
|
589
589
|
*/
|
|
590
590
|
function installPromiseErrorHandler() {
|
|
591
|
-
const cfg = config.get();
|
|
592
|
-
if (!cfg.error?.enabled || !cfg.error?.promiseError) {
|
|
593
|
-
return;
|
|
594
|
-
}
|
|
595
591
|
window.addEventListener('unhandledrejection', (event) => {
|
|
592
|
+
// 延迟检查配置(SDK 可能还在加载配置)
|
|
593
|
+
if (config.isInitialized()) {
|
|
594
|
+
const cfg = config.get();
|
|
595
|
+
if (!cfg.error?.enabled || !cfg.error?.promiseError) {
|
|
596
|
+
return;
|
|
597
|
+
}
|
|
598
|
+
}
|
|
596
599
|
let message = 'Unhandled Promise rejection';
|
|
597
600
|
let stack;
|
|
598
601
|
const reason = event.reason;
|
|
@@ -621,9 +624,6 @@ function installPromiseErrorHandler() {
|
|
|
621
624
|
});
|
|
622
625
|
reporter.reportError(errorData);
|
|
623
626
|
});
|
|
624
|
-
if (cfg.debug) {
|
|
625
|
-
console.log('[Monitor] Promise error handler installed');
|
|
626
|
-
}
|
|
627
627
|
}
|
|
628
628
|
|
|
629
629
|
/**
|
|
@@ -633,11 +633,14 @@ function installPromiseErrorHandler() {
|
|
|
633
633
|
* 安装资源错误监听
|
|
634
634
|
*/
|
|
635
635
|
function installResourceErrorHandler() {
|
|
636
|
-
const cfg = config.get();
|
|
637
|
-
if (!cfg.error?.enabled || !cfg.error?.resourceError) {
|
|
638
|
-
return;
|
|
639
|
-
}
|
|
640
636
|
window.addEventListener('error', (event) => {
|
|
637
|
+
// 延迟检查配置(SDK 可能还在加载配置)
|
|
638
|
+
if (config.isInitialized()) {
|
|
639
|
+
const cfg = config.get();
|
|
640
|
+
if (!cfg.error?.enabled || !cfg.error?.resourceError) {
|
|
641
|
+
return;
|
|
642
|
+
}
|
|
643
|
+
}
|
|
641
644
|
const target = event.target;
|
|
642
645
|
// 只处理资源加载错误(target 不是 window)
|
|
643
646
|
if (!target || target === window || !(target instanceof HTMLElement)) {
|
|
@@ -671,9 +674,6 @@ function installResourceErrorHandler() {
|
|
|
671
674
|
});
|
|
672
675
|
reporter.reportError(errorData);
|
|
673
676
|
}, true); // 使用捕获阶段
|
|
674
|
-
if (cfg.debug) {
|
|
675
|
-
console.log('[Monitor] Resource error handler installed');
|
|
676
|
-
}
|
|
677
677
|
}
|
|
678
678
|
|
|
679
679
|
/**
|
|
@@ -688,28 +688,32 @@ const originalFetch = window.fetch;
|
|
|
688
688
|
* 安装 HTTP 错误拦截
|
|
689
689
|
*/
|
|
690
690
|
function installHttpErrorHandler() {
|
|
691
|
-
const cfg = config.get();
|
|
692
|
-
if (!cfg.error?.enabled || !cfg.error?.httpError) {
|
|
693
|
-
return;
|
|
694
|
-
}
|
|
695
691
|
interceptXHR();
|
|
696
692
|
interceptFetch();
|
|
697
|
-
if (cfg.debug) {
|
|
698
|
-
console.log('[Monitor] HTTP error handler installed');
|
|
699
|
-
}
|
|
700
693
|
}
|
|
701
|
-
/**
|
|
702
|
-
* 拦截 XMLHttpRequest
|
|
703
|
-
*/
|
|
704
694
|
/**
|
|
705
695
|
* 检查是否是 SDK 自身的请求
|
|
706
696
|
*/
|
|
707
697
|
function isSdkRequest(url) {
|
|
698
|
+
if (!config.isInitialized())
|
|
699
|
+
return false;
|
|
708
700
|
const cfg = config.get();
|
|
709
701
|
if (!cfg.dsn)
|
|
710
702
|
return false;
|
|
711
703
|
return url.includes(cfg.dsn);
|
|
712
704
|
}
|
|
705
|
+
/**
|
|
706
|
+
* 检查 HTTP 错误监控是否启用
|
|
707
|
+
*/
|
|
708
|
+
function isHttpErrorEnabled() {
|
|
709
|
+
if (!config.isInitialized())
|
|
710
|
+
return true; // 配置未就绪时默认启用,数据会被缓存
|
|
711
|
+
const cfg = config.get();
|
|
712
|
+
return cfg.error?.enabled !== false && cfg.error?.httpError !== false;
|
|
713
|
+
}
|
|
714
|
+
/**
|
|
715
|
+
* 拦截 XMLHttpRequest
|
|
716
|
+
*/
|
|
713
717
|
function interceptXHR() {
|
|
714
718
|
XMLHttpRequest.prototype.open = function (method, url, async = true, username, password) {
|
|
715
719
|
const urlStr = url.toString();
|
|
@@ -739,11 +743,16 @@ function interceptXHR() {
|
|
|
739
743
|
this.addEventListener('loadend', function () {
|
|
740
744
|
if (!monitorData)
|
|
741
745
|
return;
|
|
746
|
+
if (!isHttpErrorEnabled())
|
|
747
|
+
return;
|
|
742
748
|
const duration = Date.now() - monitorData.startTime;
|
|
743
749
|
const status = this.status;
|
|
744
750
|
// 根据配置决定是否添加到面包屑
|
|
745
|
-
|
|
746
|
-
|
|
751
|
+
let recordMode = 'error';
|
|
752
|
+
if (config.isInitialized()) {
|
|
753
|
+
const cfg = config.get();
|
|
754
|
+
recordMode = cfg.behavior?.recordRequestBreadcrumb || 'error';
|
|
755
|
+
}
|
|
747
756
|
// 重定向(3xx)始终记录,不受配置控制
|
|
748
757
|
const isRedirect = status >= 300 && status < 400;
|
|
749
758
|
// 其他异常情况(0-网络错误、4xx-客户端错误、5xx-服务端错误)根据配置控制
|
|
@@ -790,11 +799,17 @@ function interceptFetch() {
|
|
|
790
799
|
const requestBody = typeof init?.body === 'string' ? init.body : undefined;
|
|
791
800
|
try {
|
|
792
801
|
const response = await originalFetch.call(window, input, init);
|
|
802
|
+
if (!isHttpErrorEnabled()) {
|
|
803
|
+
return response;
|
|
804
|
+
}
|
|
793
805
|
const duration = Date.now() - startTime;
|
|
794
806
|
const status = response.status;
|
|
795
807
|
// 根据配置决定是否添加到面包屑
|
|
796
|
-
|
|
797
|
-
|
|
808
|
+
let recordMode = 'error';
|
|
809
|
+
if (config.isInitialized()) {
|
|
810
|
+
const cfg = config.get();
|
|
811
|
+
recordMode = cfg.behavior?.recordRequestBreadcrumb || 'error';
|
|
812
|
+
}
|
|
798
813
|
// 重定向(3xx)始终记录,不受配置控制
|
|
799
814
|
const isRedirect = status >= 300 && status < 400;
|
|
800
815
|
// 其他异常情况根据配置控制
|
|
@@ -833,10 +848,16 @@ function interceptFetch() {
|
|
|
833
848
|
return response;
|
|
834
849
|
}
|
|
835
850
|
catch (error) {
|
|
851
|
+
if (!isHttpErrorEnabled()) {
|
|
852
|
+
throw error;
|
|
853
|
+
}
|
|
836
854
|
const duration = Date.now() - startTime;
|
|
837
855
|
// 网络错误时根据配置决定是否添加到面包屑
|
|
838
|
-
|
|
839
|
-
|
|
856
|
+
let recordMode = 'error';
|
|
857
|
+
if (config.isInitialized()) {
|
|
858
|
+
const cfg = config.get();
|
|
859
|
+
recordMode = cfg.behavior?.recordRequestBreadcrumb || 'error';
|
|
860
|
+
}
|
|
840
861
|
// 网络错误属于 error,当模式为 'all' 或 'error' 时都记录
|
|
841
862
|
if (recordMode !== 'none') {
|
|
842
863
|
context.addBreadcrumb({
|
|
@@ -892,10 +913,6 @@ const INVALID_ELEMENTS = ['html', 'body', 'head', 'meta', 'link', 'style', 'scri
|
|
|
892
913
|
* 安装白屏检测
|
|
893
914
|
*/
|
|
894
915
|
function installWhiteScreenDetector() {
|
|
895
|
-
const cfg = config.get();
|
|
896
|
-
if (!cfg.error?.enabled) {
|
|
897
|
-
return;
|
|
898
|
-
}
|
|
899
916
|
// 在页面加载完成后检测
|
|
900
917
|
if (document.readyState === 'complete') {
|
|
901
918
|
scheduleDetection();
|
|
@@ -905,9 +922,6 @@ function installWhiteScreenDetector() {
|
|
|
905
922
|
scheduleDetection();
|
|
906
923
|
});
|
|
907
924
|
}
|
|
908
|
-
if (cfg.debug) {
|
|
909
|
-
console.log('[Monitor] White screen detector installed');
|
|
910
|
-
}
|
|
911
925
|
}
|
|
912
926
|
/**
|
|
913
927
|
* 调度检测(延迟执行,给页面渲染时间)
|
|
@@ -915,6 +929,13 @@ function installWhiteScreenDetector() {
|
|
|
915
929
|
function scheduleDetection() {
|
|
916
930
|
// 延迟 1 秒检测
|
|
917
931
|
setTimeout(() => {
|
|
932
|
+
// 检测时才检查配置
|
|
933
|
+
if (config.isInitialized()) {
|
|
934
|
+
const cfg = config.get();
|
|
935
|
+
if (!cfg.error?.enabled) {
|
|
936
|
+
return;
|
|
937
|
+
}
|
|
938
|
+
}
|
|
918
939
|
const isWhiteScreen = detectWhiteScreen();
|
|
919
940
|
if (isWhiteScreen) {
|
|
920
941
|
reportWhiteScreen();
|