如何优雅地处理快递中的承诺拒绝

Hir*_*103 3 promise express es6-promise express-router

我有以下快递控制器

class ThingsController {

  static async index(req, res, next) {
    try {
      const things = await Thing.all();
      res.json(things);
    } catch(err) {
      next(err);
    }  
  }
}
Run Code Online (Sandbox Code Playgroud)

和路由器

router.route('/things').get(ThingsController.index)
Run Code Online (Sandbox Code Playgroud)

在我的应用程序中,我计划使用几个控制器来渲染结果

我不想每次都重复 try/catch 块

我的第一个解决方案是将此逻辑提取到处理承诺拒绝功能中:

const handlePromiseRejection = (handler) =>

  async (req, res, next) => {
    try{
      await handler(req, res, next);
    } catch(err) {
      next(err);
    };
  };
Run Code Online (Sandbox Code Playgroud)

现在我们可以从 ThingsController.index 中删除 try/catch 块,并且需要将路由器更改为:

router.route('/things')
  .get(handlePromiseRejection(ThingsController.index))
Run Code Online (Sandbox Code Playgroud)

但是添加handlePromiseRejection每条路线可能是一项乏味的任务,我希望有更聪明的解决方案。

你有什么想法?

156*_*223 5

使用async/awaitin 路由处理错误的正常方法是捕获错误并将其传递给catch all错误处理程序:

app.use(async (req, res) => {
  try {
    const user = await someAction();
  } catch (err) {
    // pass to error handler
    next(err)
  }
});

app.use((err, req, res, next) => {
  // handle error here
  console.error(err);
});
Run Code Online (Sandbox Code Playgroud)

使用express-async-errors包,您可以简单地throw(或不担心error从某些函数中抛出)。从文档:Instead of patching all methods on an express Router, it wraps the Layer#handle property in one place, leaving all the rest of the express guts intact.

用法很简单:

require('express-async-errors'); // just require!
app.use(async (req, res) => {
  const user = await User.findByToken(req.get('authorization')); // could possibly throw error, implicitly does catch and next(err) for you

  // throw some error and let it be implicitly handled !!
  if (!user) throw Error("access denied");
});

app.use((err, req, res, next) => {
  // handle error
  console.error(err);
});
Run Code Online (Sandbox Code Playgroud)