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 回答