sobey-monitor-sdk 1.1.17 → 1.1.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.
package/dist/index.cjs.js CHANGED
@@ -427,11 +427,11 @@ if (typeof window !== 'undefined') {
427
427
  });
428
428
  }
429
429
 
430
+ var version = "1.1.19";
431
+
430
432
  /**
431
433
  * 数据上报管理
432
434
  */
433
- // SDK 版本
434
- const SDK_VERSION$1 = '1.0.0';
435
435
  /**
436
436
  * 上报器类
437
437
  */
@@ -568,7 +568,7 @@ class Reporter {
568
568
  pageUrl: getPageUrl(),
569
569
  timestamp: Date.now(),
570
570
  userAgent: getUserAgent(),
571
- sdkVersion: cfg.version || SDK_VERSION$1,
571
+ sdkVersion: cfg.version || version,
572
572
  };
573
573
  }
574
574
  /**
@@ -843,21 +843,43 @@ function shouldReportHttpError(status) {
843
843
  // 获取配置的状态码列表
844
844
  if (!config.isInitialized()) {
845
845
  // 配置未初始化时,不上报
846
+ console.log(`[Monitor] shouldReportHttpError: config not initialized, status=${status}, returning false`);
846
847
  return false;
847
848
  }
848
849
  const cfg = config.get();
849
850
  const httpErrorStatusCodes = cfg.error?.httpErrorStatusCodes;
850
851
  // 如果未配置状态码列表或为空数组,则不上报任何HTTP错误
851
852
  if (!httpErrorStatusCodes || httpErrorStatusCodes.length === 0) {
853
+ console.log(`[Monitor] shouldReportHttpError: no httpErrorStatusCodes configured, status=${status}, returning false`);
852
854
  return false;
853
855
  }
854
856
  // 只检查状态码是否在配置的列表中
855
857
  const shouldReport = httpErrorStatusCodes.includes(status);
856
- if (cfg.debug) {
857
- console.log(`[Monitor] HTTP status ${status} shouldReport: ${shouldReport}, configured codes:`, httpErrorStatusCodes);
858
- }
858
+ // 临时强制输出调试日志
859
+ console.log(`[Monitor] HTTP status ${status} shouldReport: ${shouldReport}, configured codes:`, httpErrorStatusCodes);
859
860
  return shouldReport;
860
861
  }
862
+ /**
863
+ * 根据错误类型判断是否应该上报网络类错误(status = 0)
864
+ * @param errorType 错误类型:'timeout' | 'abort' | 'network'
865
+ * @returns 是否应该上报
866
+ */
867
+ function shouldReportNetworkTypeError(errorType) {
868
+ if (!config.isInitialized()) {
869
+ return false;
870
+ }
871
+ const cfg = config.get();
872
+ const errorConfig = cfg.error;
873
+ switch (errorType) {
874
+ case 'timeout':
875
+ return errorConfig?.reportTimeoutError === true;
876
+ case 'abort':
877
+ return errorConfig?.reportAbortError === true;
878
+ case 'network':
879
+ default:
880
+ return errorConfig?.reportNetworkError === true;
881
+ }
882
+ }
861
883
  /**
862
884
  * 获取嵌套对象的字段值
863
885
  * @param obj 对象
@@ -976,7 +998,20 @@ function interceptXHR() {
976
998
  if (monitorData) {
977
999
  monitorData.startTime = Date.now();
978
1000
  monitorData.requestBody = typeof body === 'string' ? body : undefined;
1001
+ monitorData.errorType = undefined; // 初始化错误类型
979
1002
  }
1003
+ // 监听超时事件
1004
+ this.addEventListener('timeout', function () {
1005
+ if (monitorData) {
1006
+ monitorData.errorType = 'timeout';
1007
+ }
1008
+ });
1009
+ // 监听取消事件
1010
+ this.addEventListener('abort', function () {
1011
+ if (monitorData) {
1012
+ monitorData.errorType = 'abort';
1013
+ }
1014
+ });
980
1015
  this.addEventListener('loadend', function () {
981
1016
  if (!monitorData)
982
1017
  return;
@@ -990,6 +1025,11 @@ function interceptXHR() {
990
1025
  // 其他异常情况(0-网络错误、4xx-客户端错误、5xx-服务端错误)根据配置控制
991
1026
  const isError = status === 0 || status < 200 || status >= 400;
992
1027
  const shouldRecord = isRedirect || recordMode === 'all' || (recordMode === 'error' && isError);
1028
+ // 确定错误类型
1029
+ let errorType;
1030
+ if (status === 0) {
1031
+ errorType = monitorData.errorType || 'network';
1032
+ }
993
1033
  if (shouldRecord) {
994
1034
  context.addBreadcrumb({
995
1035
  type: 'request',
@@ -999,12 +1039,22 @@ function interceptXHR() {
999
1039
  url: monitorData.url,
1000
1040
  status,
1001
1041
  duration,
1042
+ errorType,
1002
1043
  },
1003
1044
  });
1004
1045
  }
1005
1046
  const responseBody = this.responseText?.substring(0, 1000);
1006
- // HTTP 错误(根据配置的状态码列表过滤)
1007
- if (shouldReportHttpError(status)) {
1047
+ // HTTP 错误上报逻辑
1048
+ let shouldReport = false;
1049
+ if (status === 0) {
1050
+ // status=0 使用独立的网络错误配置
1051
+ shouldReport = shouldReportNetworkTypeError(errorType);
1052
+ }
1053
+ else {
1054
+ // 其他状态码使用 httpErrorStatusCodes 配置
1055
+ shouldReport = shouldReportHttpError(status);
1056
+ }
1057
+ if (shouldReport) {
1008
1058
  reportHttpError({
1009
1059
  method: monitorData.method,
1010
1060
  url: monitorData.url,
@@ -1012,6 +1062,7 @@ function interceptXHR() {
1012
1062
  duration,
1013
1063
  requestBody: monitorData.requestBody,
1014
1064
  responseBody,
1065
+ errorType,
1015
1066
  });
1016
1067
  }
1017
1068
  else if (status >= 200 && status < 300 && detectBusinessError(responseBody)) {
@@ -1104,6 +1155,22 @@ function interceptFetch() {
1104
1155
  }
1105
1156
  catch (error) {
1106
1157
  const duration = Date.now() - startTime;
1158
+ const err = error;
1159
+ // 判断错误类型
1160
+ // AbortError 表示请求被取消(可能是手动取消或超时)
1161
+ // 注意:fetch 的超时通常也是通过 AbortController 实现的,所以无法区分超时和手动取消
1162
+ // 这里将 AbortError 统一标记为 'abort',如果需要区分超时,建议使用 XHR 或检查 error.message
1163
+ let errorType = 'network';
1164
+ if (err.name === 'AbortError') {
1165
+ // 尝试通过错误消息区分超时和取消
1166
+ // 一些超时实现会在 message 中包含 'timeout'
1167
+ if (err.message?.toLowerCase().includes('timeout')) {
1168
+ errorType = 'timeout';
1169
+ }
1170
+ else {
1171
+ errorType = 'abort';
1172
+ }
1173
+ }
1107
1174
  // 网络错误时根据配置决定是否添加到面包屑
1108
1175
  const cfg = config.isInitialized() ? config.get() : null;
1109
1176
  const recordMode = cfg?.behavior?.recordRequestBreadcrumb || 'error';
@@ -1117,17 +1184,22 @@ function interceptFetch() {
1117
1184
  url,
1118
1185
  status: 0,
1119
1186
  duration,
1120
- error: error.message,
1187
+ error: err.message,
1188
+ errorType,
1121
1189
  },
1122
1190
  });
1123
1191
  }
1124
- reportHttpError({
1125
- method,
1126
- url,
1127
- status: 0,
1128
- duration,
1129
- requestBody,
1130
- });
1192
+ // 网络错误使用独立的配置项判断是否上报
1193
+ if (shouldReportNetworkTypeError(errorType)) {
1194
+ reportHttpError({
1195
+ method,
1196
+ url,
1197
+ status: 0,
1198
+ duration,
1199
+ requestBody,
1200
+ errorType,
1201
+ });
1202
+ }
1131
1203
  throw error;
1132
1204
  }
1133
1205
  };
@@ -1136,9 +1208,23 @@ function interceptFetch() {
1136
1208
  * 上报 HTTP 错误
1137
1209
  */
1138
1210
  function reportHttpError(httpInfo, isBusinessError = false) {
1139
- const message = isBusinessError
1140
- ? `Business Error ${httpInfo.method} ${httpInfo.url}`
1141
- : `HTTP ${httpInfo.status} ${httpInfo.method} ${httpInfo.url}`;
1211
+ let message;
1212
+ if (isBusinessError) {
1213
+ message = `Business Error ${httpInfo.method} ${httpInfo.url}`;
1214
+ }
1215
+ else if (httpInfo.status === 0 && httpInfo.errorType) {
1216
+ // 对于 status 0,显示具体的错误类型
1217
+ const errorTypeLabels = {
1218
+ 'timeout': 'Timeout',
1219
+ 'abort': 'Aborted',
1220
+ 'network': 'Network Error',
1221
+ };
1222
+ const label = errorTypeLabels[httpInfo.errorType] || 'Network Error';
1223
+ message = `HTTP ${label} ${httpInfo.method} ${httpInfo.url}`;
1224
+ }
1225
+ else {
1226
+ message = `HTTP ${httpInfo.status} ${httpInfo.method} ${httpInfo.url}`;
1227
+ }
1142
1228
  const errorData = {
1143
1229
  type: isBusinessError ? 'business_error' : 'http_error',
1144
1230
  message,
@@ -1938,8 +2024,6 @@ function createUseMonitorError(React) {
1938
2024
  * 前端监控 SDK
1939
2025
  * @description 错误监控、性能监控、行为监控
1940
2026
  */
1941
- // SDK 版本
1942
- const SDK_VERSION = '1.0.0';
1943
2027
  /**
1944
2028
  * 监控 SDK 主类
1945
2029
  */
@@ -2017,7 +2101,7 @@ class MonitorSDK {
2017
2101
  // 初始化配置管理器
2018
2102
  config.init({
2019
2103
  ...finalConfig,
2020
- version: finalConfig.version || SDK_VERSION,
2104
+ version: finalConfig.version || version,
2021
2105
  }, true);
2022
2106
  this.ready = true;
2023
2107
  this.loading = false;
@@ -2194,7 +2278,7 @@ class MonitorSDK {
2194
2278
  * 获取 SDK 版本
2195
2279
  */
2196
2280
  getVersion() {
2197
- return SDK_VERSION;
2281
+ return version;
2198
2282
  }
2199
2283
  }
2200
2284
  // 导出单例