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