2014-06-17 33 views
0

我的函数acctualy管理一些环境异常。 1层重新抛出在二层的例外,但它并不重要...如何在javascript上打印Exception stacktrace?

我的问题很简单,这个工程:

throw { 
     name:"RangeWithValues", 
     message:"The result range cells must be empty", 
     //stack:e, 
     toString:function(){return (this.name + ": " + this.message);} 
    }; 

这不是:

throw { 
      name:"RangeWithValues", 
      message:"The result range cells must be empty", 
      //stack:e, 
      toString:function(){return (this.name + ": " + this.message + (this.hasOwnProperty(stack)?("\nCaused by: "+stack):""));} 
     }; 

它在google preadsheet上打印[object Object]。我想打印堆栈跟踪。 我不知道如果我需要更多的信息给你,看来我的问题是,柠简单= S

回答

0

创建一个自定义错误,而不是抛出直接对象:

function CustomError(name, message) { 
    this.name = (name || ''); 
    this.stack = (new Error()).stack; 
    this.message = (message || '') + ' ' + this.stack; 
} 

CustomError.prototype = Error.prototype; 

throw new CustomError('RangeWithValues', 'The result range cells must be empty'); 

另见stacktrace.js,框架不可知论的微型库,用于在所有Web浏览器中获取堆栈跟踪。