【问题标题】:Why javascript promises shows <anonymous> location in error logs?为什么 javascript 承诺在错误日志中显示 <anonymous> 位置?
【发布时间】:2019-01-07 07:40:36
【问题描述】:

我想在我的代码库中记录 fileName、lineNo、ColNo。为此,我正在使用 _thisLine()。 它基本上获取行号。通过创建(不抛出)错误。 但是如果我从 promise 中调用 thisLine(),这种方法就会失败

你能帮帮我吗!

    function _thisLine() {
  const e = new Error();
  const regex = /\((.*):(\d+):(\d+)\)$/;
  const match = regex.exec(e.stack.split("\n")[2]); //i dont want to change thisLine function
  return match
    ? {
        filepath: match[1],
        line: match[2],
        column: match[3]
      }
    : "NOT FOUND";
}
function without() {
  return _thisLine(); // this is ok
}
function withPromise() {
  return new Promise(function(resolve, reject) {
    var result = _thisLine(); //inside promise unable to capture current line number
    resolve(result);
  });
}
console.log("without Promise", without());
withPromise().then(function(result) {
  console.log("with    Promise", result);
});

我希望 withPromise 返回触发点位置 但由于承诺..我无法找到触发点

回答(我的解决方法)!对我有用!

  private _thisLine() {
    const e = new Error();
    // for path like - 'LogWrapper.debug (D:\\Projects\\rrr\\node\\build\\server\\log_server\\log_wrapper.js:101:14)'
    const regex1 = /\((.*):(\d+):(\d+)\)$/;
    // for path like - 'D:\\Projects\\rrr\\node\\build\\server\\http_repo\\labtest_repo.js:58:24'
    const regex2 = /(.*):(\d+):(\d+)$/;

    let match = null,
      callStackDepth = 2,
      errorExploded = e.stack.split("\n");

    while (!!!match) {
      //match both kind of path patterns
      match =
        regex1.exec(errorExploded[callStackDepth]) ||
        regex2.exec(errorExploded[callStackDepth]);

      //if not found then move to nearest path
      callStackDepth--;
    }

    return {
      filepath: match[1],
      line: match[2],
      column: match[3]
    };
  }

【问题讨论】:

  • anonymous 是指传递给Promise 的函数,即它是一个未命名(匿名)函数
  • 它仍然可以(技术上)包含该位置,不是吗?
  • @user2864740 谢谢..这让我想到..我已经更新了答案
  • @user2864740 实际上是这样,在at new Promise 上方的跟踪行中:它是文件的第 19 行第 17 列,不是吗?但是,我认为将一个堆栈帧格式化为两行是您的环境的错误。请举报。

标签: javascript typescript promise es6-promise


【解决方案1】:

我认为您需要修复您的正则表达式。 (我不是正则表达式中的 epxert)我猜您的正则表达式期望位置数据包含在括号“()”内,并且当它触发内部承诺时,因为 e.stack.split("\n")[2] 没有返回括号,执行正则表达式是返回 null。

我添加了一些 console.log 来打印出值

function _thisLine() {
       const e = new Error();
       const regex = /\((.*):(\d+):(\d+)\)$/;
       const match = regex.exec(e.stack.split("\n")[2]);
       console.log('trigger location...'+e.stack.split("\n")[2]);
       console.log('match...'+match);
       return match
         ? {
             filepath: match[1],
             line: match[2],
             column: match[3]
           }
           : "NOT FOUND";
}
function without() {
  return _thisLine(); // this is ok
}
function withPromise() {
  return new Promise(function(resolve, reject) {
    var result = _thisLine(); //inside promise unable to capture current line number
	console.log('result is ...' + result);
    resolve(result);
  });
}
console.log("without Promise", without());
withPromise().then(function(result) {
  console.log("with    Promise", result);
});

【讨论】:

    【解决方案2】:

    撤消 - 待审核

    使用干净堆栈执行的承诺结算处理程序被错误地解释为解释为什么在这样的处理程序内创建的错误对象会丢失调用堆栈跟踪数据。 p>

    数据没有丢失。

    在浏览器中记录错误。并在控制台中向下钻取,显示 new Error().stack 属性数组中存在条目。

    【讨论】:

    • 感谢您的精彩解释。你能推荐一个可以做到这一点的 Promise 库吗?
    • 请不要接受这个答案 - 这是基于_thisLine 正常工作的假设,因此错误对象中没有可用的跟踪信息。两者都证明是错误的:-(如果它解决了问题,您可以接受@Karthikeyan 的答案或发布您自己的答案。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2012-08-10
    • 1970-01-01
    • 2011-04-12
    • 1970-01-01
    • 2012-11-21
    • 1970-01-01
    • 2020-11-11
    • 2012-03-14
    相关资源
    最近更新 更多