在 JavaScript 中使用堆栈跟踪记录错误

发布于 2024-08-16 01:07:37 字数 1230 浏览 2 评论 0原文

我正在尝试在一个高效的网站上记录 javascript 错误。到目前为止,它与站点中包含的以下代码配合得很好:

function catcherr(errorMessage, url, line) {
    var parameters = "msg=" + escape(errorMessage)
            + "&url=" + escape(url)
            + "&line=" + escape(line);

    new Image().src = "/error.gif?" + parameters;

    return false;
};

window.onerror = catcherr;

我正在尝试向错误添加堆栈跟踪以获取更多信息。这基本上与以下想法一起工作,包括上面的函数:

    try { i.dont.exist += 0; } // does not exist - that's the point
    catch (e)
    {
            if (e.stack) // Firefox
            {
               // do some stuff

我使用jquery,一个简单的例子:

<script type="text/javascript">
jQuery(document).ready(function() {
    p.foo += 1; // this should throw an error
    // do stuff
});
</script>

有趣的部分是,当我在jquery的“ready”函数中出现错误时,“try { i.dont.”部分发生错误。 exit += 0; }" 不再抛出任何异常,并且引擎停止且没有任何错误。 在上面的示例中,catcherr 扩展如下,只有“1”收到警报:

function catcherr(errorMessage, url, line) {
    try { alert(1); i.dont.exist += 0; alert(4);} catch(e) { alert(5);}
    alert(2);
    var parameters = "msg=" + escape(errorMessage)
    // ...
}

当 jquery 的“ready”函数内部发生错误时,有人知道为什么会中断吗?

I'm trying to log javascript errors on a productive site. So far it worked quite well with the following code included in the site:

function catcherr(errorMessage, url, line) {
    var parameters = "msg=" + escape(errorMessage)
            + "&url=" + escape(url)
            + "&line=" + escape(line);

    new Image().src = "/error.gif?" + parameters;

    return false;
};

window.onerror = catcherr;

I'm trying to add a stack trace to the errors to get more information. This basically works with the following idea including into the function above:

    try { i.dont.exist += 0; } // does not exist - that's the point
    catch (e)
    {
            if (e.stack) // Firefox
            {
               // do some stuff

I use jquery, a simple example:

<script type="text/javascript">
jQuery(document).ready(function() {
    p.foo += 1; // this should throw an error
    // do stuff
});
</script>

The funny part is, that when I have an error inside the "ready" function of jquery, the part "try { i.dont.exist += 0; }" does not throw any exception anymore and the engine stops without any error.
With the example above, and catcherr extended as follows, only "1" gets alerted:

function catcherr(errorMessage, url, line) {
    try { alert(1); i.dont.exist += 0; alert(4);} catch(e) { alert(5);}
    alert(2);
    var parameters = "msg=" + escape(errorMessage)
    // ...
}

Anyone having an idea why this breaks, when an error occurs inside the "ready" function of jquery?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

天气好吗我好吗 2024-08-23 01:07:37

我不知道你为什么会遇到这个问题(它看起来很奇怪,我不认为这是 JQuery 吃掉你的异常的问题,因为你的警报(1)不会被触发),但我确实想提一下你的使用 Error.stack - 当调用 onerror 事件时,您没有原始错误的堆栈上下文,因此在该点获取堆栈跟踪(通过捕获您自己的错误)将不会产生一个有意义的堆栈。

但回到真正的答案 - 与其通过编写专门的损坏代码来模拟问题以便捕获错误,不如直接抛出错误怎么样? onerror 处理程序中的第一行可能是:

try { throw new Error("dummy"); } catch (e) { alert(e.stack); }

这是有效的代码,很可能不会给您带来问题。

I don't know why you have that problem (it looks very weird and I don't think its a problem with JQuery eating your exceptions, as your alert(1) wouldn't have fired), but I did want to mention your use of Error.stack - when the onerror event gets called, you do not have the stack context of the original error, so getting a stack trace at that point (by catching your own error) will not yield a meaningful stack.

But back to a real answer - instead of simulating a problem by writing specifically broken code so you can catch the error, how about just directly throwing an error? The first line in your onerror handler could be:

try { throw new Error("dummy"); } catch (e) { alert(e.stack); }

This is valid code which will more likely not cause you problems.

森末i 2024-08-23 01:07:37

最有可能的是 jQuery 将回调包装在自己的 try/catch 中并忽略错误。

Most likely jQuery is wrapping the callback in its own try/catch and ignoring the error.

橘亓 2024-08-23 01:07:37

在 try/catch 中尝试不同的异常。
例如 a=1/0 或 baddarr[5]='bad' 并查看这些是否会触发异常。
有时,“未找到对象”错误可能是由于尚未加载对象造成的,并且对这些错误的处理可能与其他异常不同。

Try a different exception inside the try/catch.
like a=1/0 or baddarr[5]='bad' and see if those trigger exceptions.
Sometimes "Object Not Found" errors may be as a result of objects not being loaded yet, and there might be different handling for those than other exceptions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文