2017-07-23 89 views
0

我已創建的錯誤情況是這樣的:HapiJS錯誤處理

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'; 
    } 
} 

當我扔在我的處理程序一樣回報reply(new InsufficientScope());這些錯誤,它總是轉化爲繁榮對象莫名其妙。 Hapi內部是否會引發每個錯誤,並將其轉換爲具有屬性的Boom對象?

因爲我希望能夠通過onPreResponse擴展點指定某些特定錯誤的行爲(即記錄是/否等)。但我無法真正發揮作用,這是我自己的錯誤,或者是Hapi引發的錯誤,因爲它們看起來都一樣。

我也不會做這樣的事情:

if (req.response instanceof InsufficientScope) { 
// do something 
} 

我也不能使用Boom.create,因爲這不會是一個實例,但只是一個普通的對象。

回答

0

您可以通過兩種方式
1)回覆做(空,新InsufficientScope())
2)onPreresponse鉤你可以像這樣

server.ext('onPreResponse', (request, reply) => { 
    if((request.response.isBoom) 
    { 
    return reply(null,new InsufficientScope()) 
    } 
    return reply.continue(); 
})