

exports.notFound = (req, res) => {
  res.status(404).json({ message: 'Route not found' });
}

exports.errorHandler = (err, req, res, next) => {
  console.error(err);

  if (err.name === 'ValidationError') {
    const messages = Object.values(err.errors).map((e) => e.message);
    return res.status(400).json({ message: messages.join(', ') });
  }

  if (err.name === 'CastError') {
    return res.status(400).json({ message: 'Invalid ID' });
  }

  const status = err.statusCode || 500;
  const message = err.message || 'Server error';

  res.status(status).json({ message });
}
