text: const ErrorResponse = require('../utils/errorResponse');
  const errorhandler = (err, req, res, next) => {
    let error = { ...err };
    error.message = err.message;
    if (err.name === 'CastError') {
      const message = `Bootcamp with ${err.value} not found`;
      error = new ErrorResponse(message, 404);
    }
    //Mongoose Duplicate Key
    if (err.code === 11000) {
      const message = 'Duplicate field value entered';
      error = new ErrorResponse(message, 400);
    }
  
    //Mongoose Validation Error
    if (err.name === 'ValidationError') {
      const message = Object.values(err.errors).map(val => val.message);
      error = new ErrorResponse(message, 400);
    }
    res.status(error.statusCode || 500).json({
      success: false,
      data: error.message || 'internal server error'
    }) 
  };
  
  module.exports = errorhandler;
  