使用window.onerror捕获错误但在控制台上看到错误 [英] Catch errors with window.onerror yet see errors on console

查看:559
本文介绍了使用window.onerror捕获错误但在控制台上看到错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网页正在使用window.onerror捕获错误,并将错误发送到服务器进行记录。这很好用。但是,我停止在Chrome和Firefox控制台日志中看到错误。这使得调试应用程序变得更加困难。有没有什么方法可以继续使用window.onerror但是在控制台上看到错误日志而不是自己写在错误上?当Chrome将错误写入控制台时(如果未使用onerror),堆栈跟踪和导航到错误源非常有帮助。

My web page is catching errors with window.onerror and than sends the error to the server for logging. This is working well. However, I stopped seeing the errors on Chrome and Firefox console logs. This makes debugging the application more difficult. Is there any way I can keep using window.onerror yet see the error logs on the console without writing it myself on the onerror? When Chrome is writing the error to the console (if onerror is not used) the stacktrace and navigation to the error source are really helpful.

推荐答案

您必须在错误处理程序的末尾返回 false ,以使默认错误处理程序(登录到控制台)运行。

You have to return false at the end of the error handler to let the default error handler (which logs to the console) run.

window.onerror = function myErrorHandler(errorMsg, url, lineNumber) {
  //Do some of your ajax requests or whatever  

  return false;
}

MDN之外:


当函数返回true时,这会阻止触发
默认事件处理程序。

When the function returns true, this prevents the firing of the default event handler.

https://developer.mozilla.org/en- US / docs / Web / API / GlobalEventHandlers.onerror

这篇关于使用window.onerror捕获错误但在控制台上看到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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