3

定義された名前空間内にないエラーを無視するようにraven-jsを構成することは可能ですか?

var Foo = Foo || {};

Foo.raiseWithinNamespace = function(){
   //bar is not defined, raises
   return bar;
}

function raiseOutOfNameSpace(){
   //bar is not defined, raises
   return bar;
}

したがってFoo.raiseWithinNamespace、キャプチャされ、raiseOutOfNameSpace無視されます。

4

2 に答える 2

2

名前空間内の各関数を、によって作成されたラッパーで簡単に置き換えることができますRaven.wrap()

// Do not catch global errors
Raven.config(..., {collectWindowErrors: false}).install()

// Helper to catch exceptions for namespace
function catchInNamespace(ns)
{
  for (var key in ns)
  {
    if (ns.hasOwnProperty(key) && typeof ns[key] == "function")
      ns[key] = Raven.wrap(ns[key]);
  }
}

// Declaration of namespace Foo
var Foo = Foo || {};
Foo.func1 = ...;
Foo.func2 = ...;
catchInNamespace(Foo);

// Using namespace
Foo.func1();   // Any exceptions here are caught by Raven.js

他の名前空間とグローバル関数からのエラーを無視するには、構成オプションが必要であることに注意してくださいcollectWindowErrors: false。これがないと、Raven.js はすべての例外を暗黙的にキャッチします。このオプションはRaven.js 1.1.0 で導入されましたが、何らかの理由でまだ文書化されていません。

于 2014-07-05T18:53:26.223 に答える
1

これは、クラス継承を使用して行うことができます。

function capture_exception_iff(){};
//Errors will only be captured in Foo and A.
var Foo = Foo || new capture_exception_iff();
var A = A || new capture_exception_iff();
var B = B || {};


function Raven_capture_exception(e){
    if(this instanceof capture_exception_iff){
        Raven.captureException(e)
    }  
}

Foo.raiseWithinNamespace = function(){
   try {
      return bar;
   } catch(e) {
      Raven_capture_exception(e)
      //it will pass the if-statement 
      //Raven.captureException(e) will be called.
   }
 }

B.raiseWithinNamespace = function(){
   try {
      return bar;
   } catch(e) {
      Raven_capture_exception(e)
      //it will not pass the if-statement 
      //Raven.captureException(e) will not be called.
   }
 }

function raiseOutOfNameSpace(){
   try {
      return bar;
   } catch(e) {
      Raven_capture_exception(e)
      //it will not pass the if-statement
      //Raven.captureException(e) will not be called.
   }
}
于 2014-06-29T20:23:08.207 に答える