in my project i written below route code for hapi

const routeConfig = {
  method: 'POST',
  path: '/user',
  config: {
    validate: {
      payload: {
        firstName: Joi.string().required(),
      }
    },
    handler
  }
}

So when I don’t pass the firstName it throws error like this

{
    "statusCode": 400,
    "error": "Bad Request",
    "message": "child \"firstName\" fails because [\"firstName\" is required]",
    "validation": {
        "source": "payload",
        "keys": [
            "firstName"
        ]
    }
}

Now, I was trying to handle the above error in catch

const handler = async(request, reply) => {
  try {
    const payload = request.payload
    const createUser = await User.create(payload)
    let token = Helpers.createJwt(createUser) 
    reply({ success: true, message: 'User created successFully', token })
  } catch(err) {
     // need to do something here to handle the error like
     if (err) {
       reply ({ error: "firstName is required", message: "Signup unsuccessfull" })
     }
  }
}

So i found the best solution for this using this below code

config: {
        validate: {
            payload: {
                firstName: Joi.string().required(),
            },
            failAction(request, reply, source, error) {
              reply({
                error: 'badRequest',
              }).code(400);
            },
        },
    },

If the Joi validation fails, it will trigger the failAction and you will be able to handle the error (sending it to log service and/or return a specific message).

It’s a bit hidden in the doc but here is the relevant part