有人知道如何在Node.js中打印堆栈跟踪吗?


当前回答

据我所知,在nodejs中打印完整的堆栈跟踪是不可能的,你可以只打印“部分”堆栈跟踪,你无法看到你从代码中的哪里来,只是异常发生的地方。这就是瑞恩·达尔在youtube视频中解释的内容。http://youtu.be/jo_B4LTHi3I为了精确,至少56:30。希望这能有所帮助

其他回答

获取函数调用者详细信息:

/**
 * @typedef {Object} TCallerInfo
 * @property {() => string} toString
 * @property {string} str Caller error stack line.
 * @property {string} file Caller file path.
 * @property {number} line Caller line.
 * @property {number} col Caller column.
 * @property {Error} error Caller error stack instance.
 */

/**
 * @returns {TCallerInfo | null}
 */
function getCallerLine() {
  const err = new Error();
  const stack = err.stack || '';
  const callerLine = stack.split(/\n\s*at\s+/g);

  if (callerLine.length >= 2) {
    const str = callerLine[3];
    const [file, line, col] = str
      .replace(/^\s*at\s+/, '')
      .replace(/^(.*):(\d+):(\d+)$/, '$1|$2|$3')
      .split(/\|/g);

    const o = {
      toString: () => str,

      get str() {
        return str;
      },

      get file() {
        return file;
      },

      get line() {
        return parseInt(line);
      },

      get col() {
        return parseInt(col);
      },

      get error() {
        return err;
      },
    };

    return o;
  } else {
    return null;
  }
}

用法:

function foo() {
  console.info(getCallerLine());
}

foo(); // Prints this line as Caller Line details.

尝试错误。captureStackTrace (constructorOpt targetObject[])。

const myObj = {};
function c() {
  // pass
}

function b() {
    Error.captureStackTrace(myObj)
    c()
} 

function a() {
    b()
}

a()

console.log(myObj.stack)

函数a和b在错误堆栈中被捕获并存储在myObj中。

据我所知,在nodejs中打印完整的堆栈跟踪是不可能的,你可以只打印“部分”堆栈跟踪,你无法看到你从代码中的哪里来,只是异常发生的地方。这就是瑞恩·达尔在youtube视频中解释的内容。http://youtu.be/jo_B4LTHi3I为了精确,至少56:30。希望这能有所帮助

如果你只想记录错误的堆栈跟踪(而不是错误消息),Node 6及以上会自动在堆栈跟踪中包含错误名称和消息,如果你想做一些自定义错误处理,这有点烦人:

console.log (error.stack.replace(错误。信息,”)

这个解决方案将只记录错误名称和堆栈跟踪(因此,例如,您可以格式化错误消息,并在代码中的其他地方以您希望的方式显示它)。

上面的例子只打印错误名称,后面跟着堆栈跟踪,例如:

Error: 
    at /Users/cfisher/Git/squashed/execProcess.js:6:17
    at ChildProcess.exithandler (child_process.js:213:5)
    at emitTwo (events.js:106:13)
    at ChildProcess.emit (events.js:191:7)
    at maybeClose (internal/child_process.js:877:16)
    at Socket.<anonymous> (internal/child_process.js:334:11)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at Pipe._handle.close [as _onclose] (net.js:498:12)

而不是:

Error: Error: Command failed: sh ./commands/getBranchCommitCount.sh HEAD
git: 'rev-lists' is not a git command. See 'git --help'.

Did you mean this?
        rev-list

    at /Users/cfisher/Git/squashed/execProcess.js:6:17
    at ChildProcess.exithandler (child_process.js:213:5)
    at emitTwo (events.js:106:13)
    at ChildProcess.emit (events.js:191:7)
    at maybeClose (internal/child_process.js:877:16)
    at Socket.<anonymous> (internal/child_process.js:334:11)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at Pipe._handle.close [as _onclose] (net.js:498:12)

现在主机上有一个专门的功能:

console.trace()