【发布时间】:2021-08-25 23:17:22
【问题描述】:
在我的 Node Express JS Web 应用程序中,我有以下函数链,其中后续 api 函数尝试调用服务函数并捕获服务函数抛出的错误。
在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 一直是空的。
【问题讨论】:
-
尝试显示
err.stack显示的内容?
标签: javascript node.js express async-await try-catch