All files / core transformData.ts

53.33% Statements 8/15
0% Branches 0/10
0% Functions 0/2
53.33% Lines 8/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 551x 1x     1x 1x 1x     1x                                                           1x         1x                    
import { ERRORTYPES } from '../common/common'
import { getLocationHref, getTimestamp } from '../utils/index'
import { ResourceErrorTarget } from '../browser/handleEvents'
import { ReportDataType } from '../types/transportData'
import { globalVar } from '../common/common'
import { Severity } from '../utils/Severity'
import { fromHttpStatus } from '../utils/httpStatus'
import { MITOHttp } from '../types/common'
 
export function httpTransform(data: MITOHttp): ReportDataType {
  let message = ''
  const { elapsedTime, time, method, traceId, type, status } = data
  if (status === 0) {
    message = elapsedTime <= globalVar.crossOriginThreshold ? 'http请求失败,失败原因:跨域限制或域名不存在' : 'http请求失败,失败原因:超时'
  } else {
    message = fromHttpStatus(status)
  }
  return {
    type: ERRORTYPES.FETCH_ERROR,
    url: getLocationHref(),
    time,
    elapsedTime,
    level: Severity.Low,
    message,
    name: `${type}--${method}`,
    request: {
      httpType: type,
      traceId,
      method,
      url: data.url,
      data: data.reqData || ''
    },
    response: {
      status,
      data: data.responseText
    }
  }
}
 
const resourceMap = {
  img: '图片',
  script: 'js脚本'
}
 
export function resourceTransform(target: ResourceErrorTarget): ReportDataType {
  return {
    type: ERRORTYPES.RESOURCE_ERROR,
    url: getLocationHref(),
    message: '资源地址: ' + (target.src.slice(0, 100) || target.href.slice(0, 100)),
    level: Severity.Low,
    time: getTimestamp(),
    name: `${resourceMap[target.localName] || target.localName}加载失败`
  }
}