最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

在跟踪堆栈错误对象中获取行号的文件名

IT培训 admin 6浏览 0评论

在跟踪堆栈/错误对象中获取行号的文件名

我在Node.js / express中改进了我的错误处理。

有没有办法获取我的代码中出现此错误的文件名和行号?

error-handler.js,使用console.trace,只是处理错误的路线......而不是错误实际发生在哪里。


class FacebookErr extends Error {
  constructor(httpCode, ...args) {
    super(...args)
    Error.captureStackTrace(this, FacebookErr);
    this.name = 'FacebookErr';
    this.date = new Date();
    this.httpCode = httpCode;
  }
}

  fetch(uri)
    .then(status)
    .then(toJson)
    .then(getUserInfo)
    .catch((err) => {
      next(new FacebookErr(500, err));
    });

Trace: FacebookErr: ReferenceError: uri is not defined
    at log (/home/one/github/dolphin/app/error-handler.js:4:11)
    at Layer.handle_error (/home/one/github/dolphin/node_modules/express/lib/router/layer.js:71:5)
    at trim_prefix (/home/one/github/dolphin/node_modules/express/lib/router/index.js:315:13)
    at /home/one/github/dolphin/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/home/one/github/dolphin/node_modules/express/lib/router/index.js:335:12)
    at Immediate.next (/home/one/github/dolphin/node_modules/express/lib/router/index.js:275:10)
    at Immediate.<anonymous> (/home/one/github/dolphin/node_modules/express/lib/router/index.js:635:15)
    at runCallback (timers.js:783:20)
    at tryOnImmediate (timers.js:743:5)
    at processImmediate [as _immediateCallback] (timers.js:714:5)
Sun Dec 24 2017 13:39:37 GMT-0600 (CST)
回答如下:

使用正则表达式...以下将为您提供堆栈跟踪中第一个文件的文件名(完整路径),行号和列号:

const [, filename, line, column ] = err.stack.match(/\/([\/\w-_\.]+\.js):(\d*):(\d*)/)

如果要获取项目中第一个文件的信息,而不是node_modules中的信息:

const regEx =  = new RegExp(`${process.cwd()}\\/(?!node_modules\\/)([\\/\\w-_\\.]+\\.js):(\\d*):(\\d*)`)
const [, filename, line, column ] = err.stack.match(regEx)

在跟踪堆栈/错误对象中获取行号的文件名

我在Node.js / express中改进了我的错误处理。

有没有办法获取我的代码中出现此错误的文件名和行号?

error-handler.js,使用console.trace,只是处理错误的路线......而不是错误实际发生在哪里。


class FacebookErr extends Error {
  constructor(httpCode, ...args) {
    super(...args)
    Error.captureStackTrace(this, FacebookErr);
    this.name = 'FacebookErr';
    this.date = new Date();
    this.httpCode = httpCode;
  }
}

  fetch(uri)
    .then(status)
    .then(toJson)
    .then(getUserInfo)
    .catch((err) => {
      next(new FacebookErr(500, err));
    });

Trace: FacebookErr: ReferenceError: uri is not defined
    at log (/home/one/github/dolphin/app/error-handler.js:4:11)
    at Layer.handle_error (/home/one/github/dolphin/node_modules/express/lib/router/layer.js:71:5)
    at trim_prefix (/home/one/github/dolphin/node_modules/express/lib/router/index.js:315:13)
    at /home/one/github/dolphin/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/home/one/github/dolphin/node_modules/express/lib/router/index.js:335:12)
    at Immediate.next (/home/one/github/dolphin/node_modules/express/lib/router/index.js:275:10)
    at Immediate.<anonymous> (/home/one/github/dolphin/node_modules/express/lib/router/index.js:635:15)
    at runCallback (timers.js:783:20)
    at tryOnImmediate (timers.js:743:5)
    at processImmediate [as _immediateCallback] (timers.js:714:5)
Sun Dec 24 2017 13:39:37 GMT-0600 (CST)
回答如下:

使用正则表达式...以下将为您提供堆栈跟踪中第一个文件的文件名(完整路径),行号和列号:

const [, filename, line, column ] = err.stack.match(/\/([\/\w-_\.]+\.js):(\d*):(\d*)/)

如果要获取项目中第一个文件的信息,而不是node_modules中的信息:

const regEx =  = new RegExp(`${process.cwd()}\\/(?!node_modules\\/)([\\/\\w-_\\.]+\\.js):(\\d*):(\\d*)`)
const [, filename, line, column ] = err.stack.match(regEx)
发布评论

评论列表 (0)

  1. 暂无评论