All files / src/core transformData.ts

100% Statements 18/18
75% Branches 9/12
100% Functions 2/2
100% Lines 18/18

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 55 56 57 585x 5x     5x 5x 5x   5x   5x 7x 42x 7x 7x 4x   3x   7x 7x                                           5x         5x 1x                  
import { ERRORTYPES } from '../common/constant'
import { getLocationHref, getTimestamp } from '../utils/index'
import { ResourceErrorTarget } from '../browser/handleEvents'
import { ReportDataType } from '../types/transportData'
import { globalVar } from '../common/constant'
import { Severity } from '../utils/Severity'
import { fromHttpStatus, SpanStatus } from '../utils/httpStatus'
import { MITOHttp } from '../types/common'
import { getRealPath } from './errorId'
 
export function httpTransform(data: MITOHttp): ReportDataType {
  let message = ''
  const { elapsedTime, time, method, traceId, type, status } = data
  const name = `${type}--${method}`
  if (status === 0) {
    message = elapsedTime <= globalVar.crossOriginThreshold ? 'http请求失败,失败原因:跨域限制或域名不存在' : 'http请求失败,失败原因:超时'
  } else {
    message = fromHttpStatus(status)
  }
  message = message === SpanStatus.Ok ? message : `${message} ${getRealPath(data.url)}`
  return {
    type: ERRORTYPES.FETCH_ERROR,
    url: getLocationHref(),
    time,
    elapsedTime,
    level: Severity.Low,
    message,
    name,
    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}加载失败`
  }
}