我在 sail 框架上使用 co 模块进行编码。
我想捕获 InvalidError 但错误日志显示“未定义”。
如何修复此代码?
Co 模块无法捕获 ErrorType 规范??
detail: function (req, res) {
co(function *() {
let errors = [];
const text = req.param('text');
if (text.length <= 0) {
throw new InvalidError('text is required');
}
}).catch((InvalidError, err) => {
sails.log.warn(err);
errors.push(err.message);
req.flash('errors', errors);
res.redirect('/somewhere/view');
}).catch((Error, err) => {
sails.log.error(err);
res.serverError(err);
});
}
错误日志在这里
warn: undefined
error: undefined
error: Sending empty 500 ("Server Error") response
最佳答案
catch
方法只接受一个参数:err
。尝试:
.catch(err => {
sails.log.warn(err);
errors.push(err.message);
req.flash('errors', errors);
res.redirect('/somewhere/view');
})
关于javascript - 如何在 Node.js 上使用 co 模块捕获异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37802453/