我已经创建了如下错误实例:
class InsufficientScope extends Error {
constructor(resource = false) {
const errorMessage = resource
? 'User does not have enough privileges to access this resource.'
: `User does not have enough privileges to access the ${resource}`;
super(errorMessage);
this.type = 'authorization_error';
this.statusCode = '403';
}
}当我在像return reply(new InsufficientScope());这样的处理程序中抛出这些错误时,它总是以某种方式被转换为Boom对象。是否在Hapi中抛出的每个错误都被转换为具有其属性的Boom对象?
因为我希望能够指定某些特定错误的行为(例如,记录yes/no等)。通过onPreResponse扩展点。但无论是我自己的错误还是Hapi抛出的错误,我都无法区分,因为它们看起来都是一样的。
我也不能做这样的事情:
if (req.response instanceof InsufficientScope) {
// do something
}我也不能使用Boom.create,因为它不是一个实例,而只是一个普通对象。
发布于 2017-09-22 19:29:44
您可以通过两种方式完成此操作
1)回复(空,新的InsufficientScope())
2)onPreresponse钩子你可以这样做
server.ext('onPreResponse', (request, reply) => {
if((request.response.isBoom)
{
return reply(null,new InsufficientScope())
}
return reply.continue();
})https://stackoverflow.com/questions/45268416
复制相似问题