如何使用window.onerror捕获所有JavaScript错误? (包括dojo) [英] how to catch ALL javascript errors with window.onerror? (including dojo)

查看:622
本文介绍了如何使用window.onerror捕获所有JavaScript错误? (包括dojo)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题是 JavaScript的后续操作:如何在弹出窗口中显示脚本错误?在哪里解释如何捕获常规的JavaScript错误:

this question is a follow-up to javascript: how to display script errors in a popup alert? where it was explained how to catch regular javascript errors using:

<script type="text/javascript">
    window.onerror = function(msg, url, linenumber) {
        alert('Error message: '+msg+'\nURL: '+url+'\nLine Number: '+linenumber);
        return true;
    }
</script>

我尝试过,发现这样的dojo erros:

I tried it and found out that dojo erros like this one:

TypeError: this.canvas is undefined         dojo.js (Row 446)

没有报告使用这种方法,这导致我的问题:

were not reported using this method, which leads me to my question:

如何报告全部使用 window.onerror (特别是dojo错误)的javascript错误?

How can I report all javascript errors using window.onerror (especially dojo errors)?

推荐答案

它可能是Dojo使用适当的错误处理方法(即try-catch块),它阻止异常冒泡并到达已经注册了错误处理程序的窗口容器。

It could be Dojo is using proper Error handling methods (i.e. try-catch blocks) which prevents the exception from bubbling up and reaching the window container, on which you have registered the error handler.

如果是这样,那么无法做到这一点。没有错误是通过catch块,所以没有错误处理程序被调用。

If so, there is no way for you to do this. No error is going past the catch block, so no error handler is being called.

正如指出的那样,您还可以使用浏览器特定的调试API,如Venkman hook和do-in-error - 一个通常仅适用于特权代码的解决方案(感谢@Sam Hanes)。

As pointed out by the comments, you can also use browser-specific debugging APIs like the Venkman hook and do break-on-error -- a solution that usually only works for privileged code (thanks to @Sam Hanes).

您还可以执行 On(require,'error',function(){}); 在DOJO的异步脚本加载器上添加错误处理 - @buggedcom的评论中提到的另一点

You can also do On(require, 'error', function () {}); to add error handling on DOJO's asynchronous script loader -- another point mentioned in the comments by @buggedcom

这篇关于如何使用window.onerror捕获所有JavaScript错误? (包括dojo)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆