Skip to content

资源加载错误捕获

收集 JavaScript、CSS 和图片等加载错误

javascript
const initResourceError = (e: Event) => {
  // 通过 e.target 确定错误是发生在哪个资源上
  const target = e.target as ResourceErrorTarget
  // img是src,link就是href
  const src = target.src || target.href
  const type = e.type
  const subType = TraceSubTypeEnum.resource
  const tagName = target.tagName
  const message = ''
  const html = target.outerHTML
  // 获取dom加载位置
  const path = getPathToElement(target)
  const behavior = getBehaviour()
  const state = behavior?.breadcrumbs?.state || []
  const reportData: ResourceErrorType = {
    type,
    subType,
    tagName,
    message,
    html,
    src,
    pageUrl: window.location.href,
    path,
    errId: getErrorUid(`${subType}-${message}-${src}`),
    state,
    timestamp: new Date().getTime()
  }
  lazyReportBatch(reportData)
}

Released under the MIT License.