/**
 * errorHandler middleware
 * - Centralized error -> JSON response.
 */

function errorHandler(err, _req, res, _next) {
  // eslint-disable-next-line no-console
  console.error(err);

  const statusCode = err.statusCode || 500;
  const code = err.code || 'INTERNAL_ERROR';

  return res.status(statusCode).json({
    error: code,
    message: err.message || 'Internal Server Error',
    details: err.details
  });
}

module.exports = { errorHandler };

