const logger = require("../config/logger"); const errorHandler = (err, req, res, next) => { logger.error(err.message, { stack: err.stack, path: req.path, method: req.method, }); // If the error is a known type, customize the response // Otherwise, send a generic server error if (err.isOperational) { // You can add an 'isOperational' property to your custom errors res.status(err.statusCode || 500).json({ error: { message: err.message, code: err.errorCode || "INTERNAL_SERVER_ERROR", }, }); } else { // For unexpected errors, don't leak details to the client res.status(500).json({ error: { message: "An unexpected error occurred.", code: "INTERNAL_SERVER_ERROR", }, }); } }; module.exports = errorHandler;