最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

如何在抛出错误时减少堆栈跟踪(指向调用站点)

运维笔记admin17浏览0评论

如何在抛出错误时减少堆栈跟踪(指向调用站点)

如何在抛出错误时减少堆栈跟踪(指向调用站点)

我有这样的功能:

function foo() {
  throw new Error('`foo` has been removed in favor of `bar`')
}

[当有人调用foo时,我希望堆栈跟踪(错误输出)指向foo的调用位置,而不是throw内的foo行。

例如,我明白了:

$ node test.js

/home/ubuntu/tmp/test.js:2
  throw new Error('`foo` has been removed in favor of `bar`')
        ^
Error: `foo` has been removed in favor of `bar`
    at foo (/home/ubuntu/tmp/test.js:2:9)
    at Object.<anonymous> (/home/ubuntu/tmp/test.js:5:1)
    ...

我该如何获得呢?

$ node test.js

/home/ubuntu/tmp/test.js:5
  foo()
  ^
Error: `foo` has been removed in favor of `bar`
    at Object.<anonymous> (/home/ubuntu/tmp/test.js:5:1)
    ...
回答如下:

[步骤1:定义自定义错误对象。有关更多信息:A String is not an Error。

function CustomError (msg) {
  Error.call(this);

  // By default, V8 limits the stack trace size to 10 frames.
  Error.stackTraceLimit = 10;

  // Customizing stack traces
  Error.prepareStackTrace = function (err, stack) {
    return stack;
  };

  Error.captureStackTrace(this, arguments.callee);

  this.message = msg;
  this.name = 'CustomError';
};

CustomError.prototype.__proto__ = Error.prototype;

第2步:使用Domain捕获未捕获的错误。

function foo() {
  throw new CustomError('`foo` has been removed in favorof `bar`');
};

var d = require('domain').create();

d.on('error', function(err) {
    /*
     * customize the output here.
     */
});

d.run(function() {
  foo();
});

第3步:定制输出。结构化堆栈跟踪是CallSite对象的数组,每个对象代表一个堆栈框架。一个CallSite对象定义these methods。

  for(var index=0; index<err.stack.length; index++){
    var frame = err.stack[index];

    var unit = frame.getFunctionName() || frame.getMethodName();
    if (unit === null) {
      unit = 'function()';
    } else {
      unit += '()'
    }

    if (index === 0) {
      console.error('%s:%d:%d\n  %s\n  ^',
        frame.getFileName(),
        frame.getLineNumber(),
        frame.getColumnNumber(),
        unit);

      console.error('Error: ' + err.message);

    } else {
      console.error('    at %s (%s:%d:%d)',
        unit,
        frame.getFileName(),
        frame.getLineNumber(),
        frame.getColumnNumber());
    };
  }; // END. stack trace

运行该程序,我们得到以下输出:

/home/ray/dev/test/error.js:57:9
  foo()
  ^
Error: `foo` has been removed in favorof `bar`
    at function() (/home/ray/dev/test/error.js:53:3)
    at b() (domain.js:183:18)
    at Domain.run() (domain.js:123:23)
    at function() (/home/ray/dev/test/error.js:52:3)
    at Module._compile() (module.js:456:26)
    at Module._extensions..js() (module.js:474:10)
    at Module.load() (module.js:356:32)
    at Module._load() (module.js:312:12)
    at Module.runMain() (module.js:497:10)
发布评论

评论列表(0)

  1. 暂无评论