如何在javascript上打印异常堆栈跟踪?

时间:2014-06-17 02:59:48

标签: javascript logging google-sheets custom-exceptions

我的函数acctualy管理一些包含的异常。第1层重新抛出第2层上的异常,但它并不重要......

我的问题很简单:

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

1 个答案:

答案 0 :(得分: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浏览器中获取堆栈跟踪。

相关问题