bulletproof-nodejs icon indicating copy to clipboard operation
bulletproof-nodejs copied to clipboard

Catch error when usign celebate liblary

Open hubochkin opened this issue 4 years ago • 2 comments

Hello, in your routes you have such file as auth.ts And we're using celebrate library to check if all data from front-end in the right format.

route.post(
    '/signup',
    celebrate({
      body: Joi.object({
        name: Joi.string().required(),
        email: Joi.string().required(),
        password: Joi.string().required(),
      }),
    }),

If my name is number or another different type (not string) I have a response from the server like this.

{
    "errors": {}
}

I would like to see response something like this

{
    "errors": {"Wrong json data type"}
}

How can I do this ?

Thank you!

hubochkin avatar Nov 13 '19 15:11 hubochkin

Hi @hubochkin

I think what is missing is the Error Handler for Celebrate

Check on your express setup, that you have the Error Handler set after the routes, something like this:

const routes = require('../api/routes')
const { errors } = require('celebrate');

const app = express()
app.use('/api', routes())
app.use(errors());

You can read more about it on the documentation of Celebrate https://github.com/arb/celebrate#example-usage

santiq avatar Nov 13 '19 15:11 santiq

@santiq Thank you! It's really helpful for me. Now you can add it to your blueprint, or I can create a merge request later.

hubochkin avatar Nov 13 '19 18:11 hubochkin