【问题标题】:Why does Chrome onerror handler not return full error messages?为什么 Chrome onerror 处理程序不返回完整的错误消息?
【发布时间】:2019-03-14 14:59:10
【问题描述】:

Chrome 输出“脚本错误”。和 Firefox 输出“ReferenceError: d is not defined”

在两个浏览器的控制台中运行以下代码并注意区别。
为什么 Chrome 的消息不像 Firefox 那样具有描述性?如何从 chrome 中获取完整的错误消息?

代码包含在 setTimeouts 中,以便它们可以在相同的上下文中运行,以便控制台运行和输出。当作为脚本运行时也会出现此问题。

// custom global error handler
setTimeout(() => {
  window.onerror = function(message) {
    console.log("Error message: ", message)
    return false
  }
})

// create an error
setTimeout(() => {
  d;
})

【问题讨论】:

  • @Ivar 是的,chrome 的错误处理确实显示了完整的消息,但问题是自定义 onerror 处理程序的消息不是 chrome 中的完整消息。
  • 我做了同样的结果,但我意识到这是错误消息,而不是记录的内容。
  • MDN does note 那个“脚本错误”。当脚本从不同的来源加载时会报告,以防止信息泄漏。看起来两种浏览器对不同来源的理解不同。
  • 我看到了 MDN 描述的脚本错误。它不应该适用于这种情况。此行为与 chrome 文档不一致,例如:developers.google.com/web/tools/chrome-devtools/console/…
  • 该页面没有提及在控制台中执行那段代码的任何内容。它作为该文档的一部分执行。 (以前)可运行的 sn-p 在 Chrome 中也显示该消息正常。

标签: javascript google-chrome firefox error-handling onerror


【解决方案1】:

对于遇到此问题的其他任何人。众所周知,由于 webpack url 方案,webpack can interfere 在 chrome 中带有 window.onerror。在这种情况下,使用 chrome 69.03 和 Webpack 3.12.0。

我找不到这些版本的解决方法,但是当从构建而不是在开发模式下运行应用程序时,onerror 会报告正确的错误。

【讨论】:

    猜你喜欢
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-06
    • 1970-01-01
    • 2021-10-18
    相关资源
    最近更新 更多