How to throw an error inside the pre handler in Hapi.js

时间:2018-09-18 20:17:33

标签: javascript node.js rest hapijs

I started using v17 of Hapi.js and I am running into issues when using the pre-handler.

I want to save a user into a database, but first I use the pre-handler to check if a user already exists. If the user exists, I want to throw an error. The structure of my route is as so...

module.exports = {
    method: "POST",
    path: "/users",
    config: {
        auth: false,
        pre: [{ method: verify_unique_user}],
        handler: create_user.create
    }
}

The content of verify_unique_user is...

async function verify_unique_user(req, h) {

    await User.findOne({
        $or: [{email: req.payload.email}, {username: req.payload.username}]
    },
    (err, user) => {

        if (user) {

            // Check if username exists.
            if (user.username === req.payload.username) {
                throw Boom.badRequest("Username taken!");
            }

            // Check if email exists.
            if (user.email === req.payload.email) {
                throw Boom.badRequest("Email taken!");
            }
        }

    });

    return req;
}

Let's assume the user already exists in the database. Then an error will be thrown from either of the if statements. When this happens, I get the following error...

events.js:167

 throw er; // Unhandled 'error' event

 ^

Error: Username taken! at User.findOne (/Users/ericbolboa/Desktop/Warble/server/src/users/util/user_function.js:16:16)

This crashed my server. This is not what I want. If I throw an error in my handler function, the response looks like this...

{
    "statusCode": 400,
    "error": "Bad Request",
    "message": "error"
}

But whenever I throw an error in the pre-handler, my server crashes. How can I throw errors properly?

2 个答案:

答案 0 :(得分:1)

不确定这是否是问题的根源,但是您可以简化异步/等待而不是使用回调

async function verify_unique_user(req, h) {

    const user = await User.findOne({
        $or: [{email: req.payload.email}, {username: req.payload.username}]
    });

    // Check if username exists.
    if (user.username === req.payload.username) {
        throw Boom.badRequest("Username taken!");
    }

    // Check if email exists.
    if (user.email === req.payload.email) {
        throw Boom.badRequest("Email taken!");
    }

    return req;
}

答案 1 :(得分:1)

看看路由的工具包(h)和options.response.failAction

一条路线可以在选项中设置response.failAction。在那里,您可以设置错误消息的格式,并发送响应,但是您可以。其中包括预处理程序引发的错误。

编辑:每个预处理程序都可以有自己的'failAction'处理程序。如果您想终止链条,则必须执行response(...).takeover()