如何使用 window.onerror 捕获所有 javascript 错误?(包括道场)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8446861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:02:50  来源:igfitidea点击:

how to catch ALL javascript errors with window.onerror? (including dojo)

javascripterror-handlingdojo

提问by jaronimoe

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:

这个问题是javascript: how to display script errors in a popup alert? 其中解释了如何使用以下方法捕获常规 javascript 错误:

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

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:

没有使用这种方法报告,这让我想到了我的问题:

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

如何使用(尤其是 dojo 错误)报告所有javascript 错误window.onerror

采纳答案by Milad Naseri

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.

可能是 Dojo 使用了正确的错误处理方法(即 try-catch 块),以防止异常冒泡并到达您已在其上注册错误处理程序的窗口容器。

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

如果这样,你没有办法做到这一点。没有错误通过 catch 块,因此没有调用错误处理程序。

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).

正如评论中指出的那样,您还可以使用特定于浏览器的调试 API,例如 Venkman 钩子并执行错误中断——一种通常仅适用于特权代码的解决方案(感谢 @Sam Hanes)。

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

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

回答by SANJAY PANDYA

you can write code like this:

你可以写这样的代码:

var goErrHandler=window.onerror;
goErrHandler= function(msg, url, linenumber) {
console.log('Error message: '+msg+'\nURL: '+url+'\nLine Number: '+linenumber);
return true;
}

goErrHandler();

so in console you'll see some thing like this :

所以在控制台你会看到这样的事情:

Error message: undefined
URL: undefined 
Line Number: undefined

回答by dexbol

The better solution is to use try/catch, e.g.

更好的解决方案是使用 try/catch,例如

try{
    if(a=='a'){

    }
}catch(e){
    alert(e);
    //or send to server
    new Image().src='errorReport.php?e='+e;
}

Google Plus seems to use this.

Google Plus 似乎使用了这个。