Koa中间件里怎么正确处理异步错误?
我在写Koa的中间件时,遇到异步操作出错没法被外层catch住的问题。
比如下面这段代码,fetch失败了但程序直接崩溃,没进我的错误处理中间件:
app.use(async (ctx, next) => {
await next();
if (ctx.status === 404) {
ctx.body = 'Not Found';
}
});
app.use(async (ctx, next) => {
const res = await fetch('https://invalid.url');
ctx.body = await res.text();
});
是不是得手动try/catch每个async函数?有没有更优雅的办法?
暂无解答