app.get('/someFunction', someFunction);
async function someFunction(req, res) {
try {
await functionWithError(parameter);
res.send('success');
} catch (err) {
console.log(err); //works
res.send(err); //does not
}
}
async function functionWithError(parameter) {
return (result = await query('SELECT * from table where column = ?', [
parameter,
]));
}
functionWithError中的错误是“查询”没有定义。在“尝试-捕捉”中,正确地捕捉火灾,但是res.send(err)是空的,但是console.log(err)工作。
发布于 2022-07-14 04:43:47
对于JSON错误,可能是循环结构吗?我想,因为我看不到您的query
函数。
你也可以试试console.log(JSON.stringify(err)) //works
吗?
如果您得到了// ⛔️ TypeError: Converting circular structure to JSON
的错误,那么是循环结构问题阻止您只是“发送”,因为您不能发送一个循环对象。
要仍然希望发送循环对象,请参阅:https://github.com/WebReflection/flatted#flatted
发布于 2022-07-14 04:44:39
这里抛出的错误对象应该使用err.name或err.message。
参考资料:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error
https://stackoverflow.com/questions/72980195
复制相似问题