异步-等待JavaScript:无法从错误对象捕获错误详细信息
在我的Node Express JS web app中,我有以下函数链,其中后续api函数尝试调用服务函数并捕获服务函数抛出的错误 在异步-等待JavaScript:无法从错误对象捕获错误详细信息,javascript,node.js,express,async-await,try-catch,Javascript,Node.js,Express,Async Await,Try Catch,在我的Node Express JS web app中,我有以下函数链,其中后续api函数尝试调用服务函数并捕获服务函数抛出的错误 在FWBDataExtracService.js const FWBDataExtracService = { getFWBData: async () => { ... ... // Check if .tabledata exists; If not, throw an error to be caught in t
FWBDataExtracService.js
const FWBDataExtracService = {
getFWBData: async () => {
... ...
// Check if .tabledata exists; If not, throw an error to be caught in the calling function.
if ($('.tabledata1').length == 0) {
console.error("DOM element .tabledata not found on the page.");
throw new Error("DOM element .tabledata not found on the page.");
}
... ...
}
}
在api.js
中,路由器函数试图调用FWBDataExtracService.getFWBData函数,然后捕获错误
const FWBDataExtracService = require('../services/FWBDataExtracService')
router.get('/GetDataFromFWB', async (req, res, next) => {
try {
.... ....
// ### THIS WILL FAIL AND AN ERROR WILL BE THROWN
const FWBJson = await FWBDataExtracService.getFWBData();
.... ....
} catch(err) {
// ### ERR IS ALWAYS EMPTY
console.log("*", JSON.stringify(err));
return res.send({'Error': JSON.stringify(err)});
}
})
由于模拟了错误,我希望捕获错误并打印错误消息。但是
err
始终为空。未指定错误属性的精确位置。在某些环境中,它位于错误对象本身上——在某些环境中,它位于原型上,或者是一个getter,或者类似的东西
JSON.stringify
将只迭代可枚举的自身属性。在Chrome中,.message
属性是不可枚举的,因此在字符串化时不包括它:
const e=新错误('foo');
log(Object.getOwnPropertyDescriptor(e,“message”)代码>尝试显示错误堆栈
显示的内容?