【问题标题】:How can I print an Exception stacktrace on javascript?如何在 javascript 上打印异常堆栈跟踪?
【发布时间】:2014-08-06 22:48:26
【问题描述】:

我的函数实际上管理了一些涉及的异常。第 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

【问题讨论】:

    标签: javascript logging google-sheets custom-exceptions


    【解决方案1】:

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

    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 浏览器中获取堆栈跟踪。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-04
      • 2010-09-13
      • 1970-01-01
      • 2011-01-18
      • 2013-09-21
      • 2011-05-15
      • 2012-01-03
      相关资源
      最近更新 更多