【发布时间】:2017-12-24 19:57:00
【问题描述】:
我正在改进 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)
【问题讨论】:
-
我不完全确定我理解这个项目的结构...
FacebookErr是在哪里定义的?错误发生在哪里?请添加您的项目结构和以上信息。 -
定义为
class FacebookErr extends Error,见原帖。所有给定的信息应该足够了。 -
很明显这是正确的类定义,我在问这个类定义在哪里。
FacebookErr位于哪个文件中?这与实际抛出错误的位置有什么关系?
标签: javascript node.js express error-handling