Function always throwing a BadRequest error

Viewed 24

I'm trying to post data using expressJs of NodeJs. I've created functions to handle errors in a proper way yet i'm getting a BadRequest error whenever I try to post data.

Error: throw new CustomError.BadRequestError('All fields are required, please verify your data!')

Here is my function:

const createTaskController = async (req, res) => {
  const {name, completed} = req.body;
  console.log(req.body);
  if ( !name || !completed) {
    throw new CustomError.BadRequestError('All fields are required, please verify your data!')
  }
    const task = await createTaskService({...req.body});
    res.status(StatusCodes.CREATED).send({msg: 'Task created successfully!'})
};

The BadRequest code:

class BadRequestError extends CustomAPIError {
    constructor(message) {
        super(message)
        this.StatusCodes = StatusCodes.BAD_REQUEST
    }
}

The CustomError code:

const CustomAPIError = require('./custom-api')
const UnauthenticatedError = require('./unathenticated')
const NotFoundError = require('./not-found')
const BadRequestError = require('./bad-request')


module.exports = {
    CustomAPIError,
    UnauthenticatedError,
    NotFoundError,
    BadRequestError
}

Thanks in advance!

0 Answers
Related