在处理程序中抛出错误时不调用 onPostHandler 扩展

onPostHandler extension not called when an error is thrown in the handler

我正在从 hapi14 升级到 hapi17,当在处理程序中抛出或返回错误时 onPostHandlers 没有被调用,这在 16 或 17 中都没有被列为重大更改。

我相信这是从回调更改为异步的结果,但我很想确认它或找出问题是什么阻止了在抛出错误时调用 onPostHandler 扩展处理程序。

const Boom = require('boom');

const extensions = {
  handleOnPostHandler: function (request, h) {
    console.log('we hit it wooo');
    return h.continue;
  }
};

const operations = {
  error: {
    description: 'Endpoint that simulates errors',
    auth: false,
    handler: function (request, h) {
      // comment me out to call the onPostHandler
      throw new Boom('an error');

      // uncomment me to call the onPostHandler
      // return 'potato';
    }
  }
};

exports.register = function (server) {
  server.ext('onPostHandler', extensions.handleOnPostHandler);
  server.route({ method: 'GET', path: '/debug/error', config: operations.error });
};

我是否遗漏了一个严重的错误或者这是预料之中的错误?

因为您在请求结束之前抛出错误?只需来自您的处理程序的 return 然后您的 handleOnPostHandler 将被调用。

life cycle methods 的列表中显示

Route handler

executes the route handler.

onPostHandler

the response contained in request.response may be modified (but not assigned a new value). To return a different response type (for example, replace an error with an HTML response), return a new response value.

但是您不允许您的处理程序转到 onPostHandler,您正在抛出一个错误。它接管响应。

这是一项未记录的功能,已在 Hapi6 中删除,因此升级到 >16 时无法模拟此功能。