从firefox捕获错误事件消息 [英] catching error event message from firefox

查看:167
本文介绍了从firefox捕获错误事件消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到一个方法来捕获firefox下的错误消息:

  window.addEventListener(error handleException,false); 

...

函数handleException(e){
alert(e);
返回false;
}

...

< script>
throw new Error('sdasd');
< / script>

这很好地输入了 handleException 方法 e 参数是firefox 下的错误事件,我不知道如何获取关联的消息即可。
例如,在chrome中,我通过 e.message 获取消息,因为在错误冒泡到没有被捕获之后,窗口级别发生了自动错误(请参阅这个小提琴:最终错误是未捕获),其中包含我手动提出的原始错误。 / p>

所以要在firefox下有相同的行为(如果你在firefox下运行小提琴,你会看到消息是undefined)我发现了一个解决方法,错误提升功能设置手动最后错误架构:

  function err(I_sText){
g_lastManualError = new误差(I_sText);
throw g_lastManualError; //这个变量是全局的,所以我可以从任何地方获取消息
}

做$ code抛出新的错误(..)我只调用 err(..)。这是非常有用的,至少对于用户定义的异常,这是我最大的担忧。在我的处理程序中 handleException 我正在咨询全局变量。



你知道我可以做什么吗?我不满意这个解决方案。



谢谢
S。

解决方案

我将您的代码修改为演示:

  function handleException(e){
console.log(e);
alert(e);
返回false;
}
window.addEventListener(error,handleException,false);

try {
throw new Error('sdasd');
}
catch(e){
console.log(e)
}
console.log('exception exception'
throw new Error('foo');
console.log('exception exception'之后);

运行此代码(在Firebug中)向我显示:

 错误:sdasd 
[打破此错误]

过滤的chrome url chrome:// firebug / content / console / commandLineExposed。 js

comman ... osed.js(第175行)
<系统>

异常后1

错误:foo`throw new Error('foo');`@ 14
pre>

如果您想要捕获错误,请使用 try {...} catch {...} 。看起来你只是绑定到一个错误事件,所以你抛出的异常仍然传播到窗口,并告诉JS引擎停止。在Chrome中运行此代码,您将看到您从未看到异常2之后,但您会看到异常1之后。



异常的目的(由 throw 创建)是停止代码执行,除非有代码来处理该特定的异常。您可以在尝试MDN页面上看到一个示例-catch



编辑:我刚刚发现,您可能正在尝试捕获一个jQuery错误事件。如果是这种情况,您的事件绑定是正确的,但您不应该使用 throw



编辑2 :我应该早点注意到,但是您尝试使用 window.addEventListener 收听DOM错误事件。错误事件不会中断执行。例如:



替换代码 throw new Error('sdasd'); 与此代码:

  var customEvent = new CustomEvent('error')

window.dispatchEvent(customEvent);

这应该可以解决你的问题。我使用自定义事件的MDN页面作为参考。


I can't find a way to catch the error message under firefox:

window.addEventListener("error", handleException, false);

...

function handleException(e) {   
    alert(e);
    return false;
}

...

<script>
throw new Error('sdasd');
</script>

This enters very well the handleException method however the e parameter is an error event under firefox and I don't know how to get the associated message. In chrome for instance, I get either the message through e.message because after the error bubbles up to not being caught, there's an automatic error fired at window level (See this fiddle: the final error is "Uncaught") that contains the original error that I raised manually.

So to have the same behaviour under firefox (if you run the fiddle under firefox you'll see that the message is "undefined") I found a workaround consisting in encapsulating an error raising function to setup a manual "last error" architecture:

function err(I_sText) { 
    g_lastManualError = new Error(I_sText);
    throw g_lastManualError; //this variable is global so I can get the message from anywhere
}

So instead of doing throw new Error(..) I only call err(..). That works, at least for user defined exceptions, which are my biggest concern. In my handler handleException I'm consulting the global variable.

Do you know how I could do otherwise? I'm not happy with this solution.

Thank you, S.

解决方案

I modified your code a little as a demo:

function handleException(e) { 
    console.log(e);
    alert(e);
    return false;
}
window.addEventListener("error", handleException, false);

try {
  throw new Error('sdasd');
}
catch (e) {
  console.log(e)
}
console.log('after exception 1');
throw new Error('foo');
console.log('after exception 2');

Running this code (in Firebug) showed me this:

Error: sdasd
[Break On This Error]   

Filtered chrome url chrome://firebug/content/console/commandLineExposed.js

comman...osed.js (line 175)
<System>

after exception 1

"Error: foo `    throw new Error('foo');` @14"

If you're trying to catch an error, use try {...} catch { ...}. It looks like you're just binding to an error event, so the exception you're throwing will still propagate up to window and tell the JS engine to halt. Run this code in Chrome, you'll see that you never see "after exception 2", but you will see "after exception 1".

The purpose of exceptions (created by throw) is to stop code execution unless there's code made to handle that particular exception. You can see an example on the MDN page for try-catch

Edit: it just occurred to me that you might be trying to catch a jQuery error event. If this is the case, your event binding is correct but you shouldn't be testing it with throw

Edit 2: I should've noticed this sooner, but you're trying to listen for a DOM error event with window.addEventListener. Error events will not break execution. Exceptions do.

Replace your code throw new Error('sdasd'); with this code:

var customEvent = new CustomEvent('error')

window.dispatchEvent(customEvent);

That should solve your problem. I used the MDN page on custom events as a reference.

这篇关于从firefox捕获错误事件消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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