如何在不尝试捕获的情况下在全球捕获JavaScript错误

发布于 2025-02-07 00:22:58 字数 271 浏览 3 评论 0原文

我有一个带有闪电网络组件的水疗中心。我的基本要求是捕获任何类型的JavaScript异常或错误并记录它们,而无需转到每个内部组件并包装中的每一块JS代码,请尝试catch

我尝试使用window.onerror,但我没有收到正确的错误消息,它将消息以“脚本错误”提供。和第1号线,col no as 0 。看起来像其他来源策略的问题

可以提出其他有效的方法来捕获任何类型的JavaScript错误?

I have a SPA built with Lightning web components. My basic requirement is to catch any kind of JavaScript exceptions or errors and log them, without having to go to each inner component and wrap every piece of JS code in try catch.

I have tried using window.onerror but I am not getting the proper error message with this, it gives message as "Script error." and line no, col no as 0. Looks like some issue with same origin policy

Can any of you please suggest some other effective way to catch any kind of JavaScript errors globally ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

缱倦旧时光 2025-02-14 00:22:58

据我所知,这是唯一的方法。
您能提供您当前正在使用的代码吗?

我将按照我实施的方式包括这里。

<script>
        window.onerror = function (message, source, lineno, colno, error) {
            console.log(`Error: ${error.message} on line: ${lineno} \nfull message: ${error}`)
            return true;
          };

          function triggerError() {
            x();
          }

          triggerError()
</script>

输出:错误:X未在行上定义:17完整消息:ReferenceError:X未定义

As far as I know, this is the only way.
Could you please provide the code that you are currently using?

I'll include here the way that i had implemented it.

<script>
        window.onerror = function (message, source, lineno, colno, error) {
            console.log(`Error: ${error.message} on line: ${lineno} \nfull message: ${error}`)
            return true;
          };

          function triggerError() {
            x();
          }

          triggerError()
</script>

Output: Error: x is not defined on line: 17 full message: ReferenceError: x is not defined

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文