在javascript中获取导致错误的调用堆栈
问题通常是无法获取调用堆栈,这可以按照此处所述完成: http://eriwen.com/javascript/js-stack-trace/ 而是从事件处理程序访问触发事件的调用堆栈。
特别是,我对从窗口错误事件记录调用堆栈感兴趣
window.onerror = function(msg, url, line) {
//callstack // would be nice to have.
//log callstack or whatever. (note this can be done w/ ajax and service, and is not the question at hand.
}
,但我确实知道如何记录错误。 (我使用 jquery 的 .ajax
和一个服务)
浏览器会让这成为可能吗?目前可以吗?也许我的处理方式是错误的。如何添加一个简单的函数(即不修改代码库中的所有函数)来检测何时出现错误,并记录调用堆栈。
感谢您迄今为止的回答,如果问题最初措辞不当,我们深表歉意。
The problem is not getting the callstack in general, which can be done as described here:
http://eriwen.com/javascript/js-stack-trace/
but rather in accessing the callstack that triggered the event, from the handler of the event.
In particular I'm interested in logging the callstack from the window error event
window.onerror = function(msg, url, line) {
//callstack // would be nice to have.
//log callstack or whatever. (note this can be done w/ ajax and service, and is not the question at hand.
}
but I do know how to log the error. (I use jquery's .ajax
, and a service)
Will browsers make this possible ever? Is it currently possible? Maybe I am going about this the wrong way. How can I add a simple function (i.e. not modify all the functions in my codebase) to detect whenever there is an error, and also log the call stack.
Thanks for the answers so far and sorry if the question was initially poorly worded.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Error 对象具有非标准 stack Mozilla 上的属性,它似乎也适用于 Google Chrome,而不是 IE9。
请参阅小提琴:http://jsfiddle.net/Cq5RJ/
The Error object has a non-standard stack property on Mozilla, it seems to work in Google Chrome too, not IE9.
See the fiddle: http://jsfiddle.net/Cq5RJ/
这在很大程度上取决于您想要对调用堆栈执行什么操作,即您所说的“记录调用堆栈或其他内容”的含义。
出于安全原因,浏览器显然不太可能允许页面提供 JavaScript 指令以在本地计算机的硬盘驱动器上写入任意文件。
您可以采用的几种可能性:
在 JavaScript 中构建调用堆栈,并将其作为详细错误消息发送到服务器上的 AJAX 处理程序,您可以在日志文件中保存错误。 (这假设您在服务器上至少有一些 cgi 访问权限)
使用 console.log() 将调用堆栈写入 javascript 控制台,假设相关浏览器支持它。 (在写入之前,请确保测试 console 和 console.log 是否存在。)
将调用堆栈写入隐藏的 div,您可以使用浏览器的开发人员工具将其调整为可见。
在alert()中打印调用堆栈。 (这对开发人员来说可能很有用,但对最终用户来说会具有很强的侵入性,因此您不希望将警报留在最终部署的代码中。)
It depends a lot on what you want to do with the callstack--that is, what you mean by "log callstack or whatever."
For security reasons, it's obviously very unlikely that browsers will ever be allowing a page to give a javascript instruction to write an arbitrary file on the local machine's hard drive.
A few possibilities that you could employ:
Build the callstack in javascript, and send it as a detailed error message to an AJAX handler on the server where you can save errors in a logfile. (This assumes you have at least some cgi access on the server)
Write the callstack to the javascript console using console.log(), assuming the browser in question supports it. (Make sure to test for the existence of console and of console.log before writing.)
Write the callstack to a hidden div that you can tweak to be visible using the browser's developer tools.
Print the callstack in an alert(). (This is potentially useful as a developer, but it would be highly intrusive to an end user, so you will not want to leave the alert in the final deployed code.)