Files
SkyArtShop/backend/middleware/processHandlers.js

72 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

2026-01-04 17:52:37 -06:00
/**
* Global Process Error Handlers
* Safeguards to prevent crashes from unhandled errors
*/
const logger = require("../config/logger");
/**
* Handle uncaught exceptions
*/
process.on("uncaughtException", (error) => {
logger.error("💥 Uncaught Exception", {
error: error.message,
stack: error.stack,
});
// Give time to log before exiting
setTimeout(() => {
process.exit(1);
}, 1000);
});
/**
* Handle unhandled promise rejections
*/
process.on("unhandledRejection", (reason, promise) => {
logger.error("💥 Unhandled Promise Rejection", {
reason: reason instanceof Error ? reason.message : reason,
stack: reason instanceof Error ? reason.stack : undefined,
promise,
});
// Don't exit - log and continue
// In production, you might want to exit: process.exit(1);
});
/**
* Handle process warnings
*/
process.on("warning", (warning) => {
logger.warn("⚠️ Process Warning", {
name: warning.name,
message: warning.message,
stack: warning.stack,
});
});
/**
* Handle SIGTERM gracefully
*/
process.on("SIGTERM", () => {
logger.info("👋 SIGTERM received, shutting down gracefully");
// Give server time to close connections
setTimeout(() => {
process.exit(0);
}, 10000);
});
/**
* Handle SIGINT gracefully (Ctrl+C)
*/
process.on("SIGINT", () => {
logger.info("👋 SIGINT received, shutting down gracefully");
process.exit(0);
});
logger.info("✅ Global process error handlers registered");
module.exports = {
// Exports for testing if needed
};