node.js - Node JS异步中间件未捕获异常
问题描述
我在 NodeJS 中有一个中间件异步函数。
module.exports = function (handler) {
return async (req, res, next) => {
try {
console.log('async called...');
await handler(req, res);
}
catch(ex) {
console.log('catch...');
next(ex);
}
};
}
这是我的路线处理程序。
const asyncMiddleware = require('../middleware/async');
router.get('/', asyncMiddleware(async (req, res) => {
await db.query('SELECT * xxx FROM aircraft LIMIT 10',
function (err, rows, fields) {
if(err) throw err;
res.send(rows)
})
}));
问题是当我使用无效语法调用路由处理程序来模拟异常时,中间件的捕获没有从路由中的 throw err 捕获异常。
当我调用路由时,我会在控制台上调用异步...但没有捕获...。
有谁知道为什么会这样?
解决方案
推荐阅读
- android - 如果使用 setVariable() 方法设置 ViewModel,则 DataBinding 不起作用
- python - 我想从 xlsx 中提取 10 张表并将所有 10 张表导出为 python 中的 csv 格式
- java - 如何使用 java cucumber 将“#”作为特征文件中的有效参数传递
- python - 使用 pip 在 Windows 上安装 Fiona 错误
- postgresql - 在每日 2 小时 CPU 峰值期间,RDS PostgreSQL DB 缓慢和超时
- python - TensorFlow 和暗网 cuda 内存问题
- sql - 重复的表分区
- c# - C#程序不执行存储过程
- python - PyTest 和多行输出
- java - 无法启动蜂巢