S SmartDocs
Series: src javascript 16 lines · Updated 2026-02-03

errorHandler.js

src/middleware/errorHandler.js

/**
 * Central error handler.
 * For production, avoid leaking stack traces.
 */
function errorHandler(err, req, res, next) {
  console.error('[ErrorHandler]', err);
  const status = err.status || 500;
  res.status(status).json({
    error: {
      message: err.message || 'Internal Server Error',
      stack: process.env.NODE_ENV === 'development' ? err.stack : undefined,
    },
  });
}

module.exports = { errorHandler };

Related articles