捕获所有 javascript 未处理的异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12571650/
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-23 10:44:30  来源:igfitidea点击:

Catching all javascript unhandled exceptions

javascript

提问by jre247

I'm trying to find or figure out a way to display in an alert box all of the unhandled javascript exceptions in an application. I'd want all of this to be done on the client side, without using any server side code. I'm using MVC3 as an environment.

我正在尝试寻找或找出一种方法,将应用程序中所有未处理的 javascript 异常显示在警报框中。我希望所有这些都在客户端完成,而不使用任何服务器端代码。我使用 MVC3 作为环境。

I've been researching for the last few days and haven't found exactly what I'm looking for.

最近几天我一直在研究,并没有找到我正在寻找的东西。

I found 2 ways below that seem like they're almost what I'm looking for, except these ways are set up so that you have to pass a function name into a custom method to print the stack trace of all unhandled exceptions within that one specific function. I'm looking for a way to not have to manually pass a function name to a custom method that prints the stack trace of all of the unhandled exceptions. I'd want these custom method to just 'listen' for all unhandled exceptions within the whole application.

我发现下面的 2 种方法似乎几乎就是我正在寻找的方法,除了这些方法被设置为您必须将函数名称传递给自定义方法以打印该方法中所有未处理异常的堆栈跟踪具体功能。我正在寻找一种不必手动将函数名称传递给打印所有未处理异常的堆栈跟踪的自定义方法的方法。我希望这些自定义方法只“侦听”整个应用程序中所有未处理的异常。

http://eriwen.com/javascript/js-stack-trace/

http://eriwen.com/javascript/js-stack-trace/

Also something similar to the previous link:

还有类似于上一个链接的内容:

https://github.com/eriwen/javascript-stacktrace

https://github.com/eriwen/javascript-stacktrace

Here's the basic code from the 2nd link above that prints the stack trace of a specified javascript function:

这是上面第二个链接中的基本代码,用于打印指定 javascript 函数的堆栈跟踪:

instrumentFunction: function (context, functionName, callback) {
    context = context || window;
    var original = context[functionName];
    context[functionName] = function instrumented() {
        callback.call(this, printStackTrace().slice(4));
        return context[functionName]._instrumented.apply(this, arguments);
    };
    context[functionName]._instrumented = original;
}

function printStackTrace(options) {
    options = options || {
        guess: true
    };
    var ex = options.e || null,
        guess = !! options.guess;
    var p = new printStackTrace.implementation(),
        result = p.run(ex);
    return (guess) ? p.guessAnonymousFunctions(result) : result;
}

So, to sum this up, do you all know of any way to have some sort of 'listener' to listen for all javascript unhandled exceptions and then print them to the screen in an alert box?

所以,总而言之,你们都知道有什么方法可以让某种“侦听器”来侦听所有 javascript 未处理的异常,然后在警报框中将它们打印到屏幕上?

Thanks! Jason

谢谢!杰森

回答by Nish

You can do this by using window.onerror method.

您可以使用 window.onerror 方法来做到这一点。

window.onerror = function myErrorHandler(errorMsg, url, lineNumber) {
    alert("Error occured: " + errorMsg);//or any message
    return false;
}

回答by Fizer Khan

You can either use window.onerror, or (amazingly!) bind to the 'error' event properly:

您可以使用window.onerror, 或(令人惊讶的是!)正确绑定到“错误”事件:

window.onerror = function (message, file, line, col, error) {
   alert("Error occurred: " + error.message);
   return false;
};
window.addEventListener("error", function (e) {
   alert("Error occurred: " + e.error.message);
   return false;
})

If you want to track JavaScript errors, you can try Atatus. I work at Atatus.

如果您想跟踪 JavaScript 错误,可以尝试Atatus。我在 Atatus 工作。

回答by Javier Perez

In addition to

此外

window.onerror = function (message, file, line, col, error) {
   alert("Error occurred: " + error.message);
   return false;
};
window.addEventListener("error", function (e) {
   alert("Error occurred: " + e.error.message);
   return false;
})

You can also catch all the errors fired inside a promise callback (.then()) listening for unhandledrejectionevent

您还可以捕获在.then()侦听unhandledrejection事件的 Promise 回调 ( ) 中触发的所有错误

window.addEventListener('unhandledrejection', function (e) {
  alert("Error occurred: " + e.reason.message);
})

回答by Robert Bolton

Check out http://log4javascript.orgit is based on Log4J. If most of your code is wrapped in try/catch statements to handle exceptions you can make use of this library as a common interface for sending output to an always available "dialog box" or logging window that your end user could see. You could even have a button that performs a window.print() to print the contents of the dialog box to the printer or PDF. Good luck.

查看http://log4javascript.org它基于 Log4J。如果您的大部分代码都包含在 try/catch 语句中以处理异常,您可以将此库用作通用接口,将输出发送到您的最终用户可以看到的始终可用的“对话框”或日志记录窗口。您甚至可以有一个按钮来执行 window.print() 以将对话框的内容打印到打印机或 PDF。祝你好运。