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