Skip to content

fix(error):将addEventListener('error')和onerror合并#1

Open
lxq-mdc wants to merge 1 commit into
sunburst89757:masterfrom
lxq-mdc:master
Open

fix(error):将addEventListener('error')和onerror合并#1
lxq-mdc wants to merge 1 commit into
sunburst89757:masterfrom
lxq-mdc:master

Conversation

@lxq-mdc
Copy link
Copy Markdown

@lxq-mdc lxq-mdc commented Mar 15, 2023

问题:监听资源加载失败错误和监听js错误写了两个监听函数
原因:使用 window.addEventListener(”error",callback, true)同样可以监控到js错误,故不需要对js错误再写一个监听函数
解决方案:将监听js错误的函数合并到监听资源加载失败错误的函数中
具体修改如下:
`

  window.addEventListener(
  "error",
  (e) => {
   const target = e.target
  if (!target) return

  if (target.src || target.href) {
    const url = target.src || target.href
    lazyReportCache({
      url,
      type: "error",
      subType: "resource",
      startTime: e.timeStamp,
      html: target.outerHTML,
      resourceType: target.tagName,
      paths: e.path.map((item) => item.tagName).filter(Boolean),
      pageURL: getPageURL()
    })
  } 
 +else {
   + const { message, lineno, colno, error, filename: url } = e
   + lazyReportCache({
  +    msg: message,
   +   line: lineno,
   +   column: colno,
   +  error: error.stack,
   +   subType: "js",
   +   pageURL: url,
    +  type: "error",
    +  startTime: performance.now()
  +  })
 + }
+},
true

)
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant