0

Here is a part of my code I am trying

const boom = require('boom')
exports.checkOtp = async (req, reply) => {
    try {
        return boom.badRequest('no active otp')
    } catch (err) {
        throw boom.boomify(err)
    }
}

I am getting

{
  "statusCode": 500,
  "error": "Internal Server Error",
  "message": "no active otp"
}

boom.badRequest should fire a 400 right?

niksmac
  • 2,667
  • 3
  • 34
  • 50
  • Maybe the error has not been fire at `boom.badRequest('no active otp')`, you could add a log in `catch` block to make sure that your code does not throw any unexpected error. Or just find `no active otp` string in whole the project. – hoangdv Sep 07 '19 at 02:52
  • There is no `no active otp` anywhere else. – niksmac Sep 07 '19 at 05:16

2 Answers2

1

Boom is a module built and designed by hapi for hapi.

You should use the (unofficial) fastify-boom plugin or add your status code:

const boom = require('boom')
exports.checkOtp = async (req, reply) => {
    try {
        reply.code(400)
        return boom.badRequest('no active otp')
    } catch (err) {
        reply.code(500)
        throw boom.boomify(err)
    }
}
Manuel Spigolon
  • 11,003
  • 5
  • 50
  • 73
1

why not just using throw error? and creating new error just by throw it

const err = new Error();
err.statusCode = 400;
err.message = 'message';
throw err;
de.
  • 7,068
  • 3
  • 40
  • 69
ahsan dev
  • 31
  • 2