Source: middleware/errorHandler.js

/**
 * Global error handler middleware to handle and log errors.
 * @param {Error} err - The error object.
 * @param {object} req - The request object.
 * @param {object} res - The response object.
 * @param {function} next - The next middleware function in the stack.
 */
function errorHandler(err, req, res, next) {
    // Check if an error occurred
    if (err) {
        // Log the error details
        console.error('Error:', err.stack || err.message);

        // Send an error response with status code 500 (Internal Server Error)
        res.status(500).json({ success: false, message: 'Internal Server Error' });
    } else {
        // If no error is passed, proceed to the next middleware
        next();
    }
}

module.exports = errorHandler;