Uncaught exceptions and unhandled rejections crash the process if not handled.
1// Uncaught synchronous exceptions2process.on("uncaughtException", (err, origin) => {3 console.error("Uncaught Exception:", err);4 // Log to monitoring service5 logger.fatal({ err, origin });6 // Graceful shutdown7 gracefulShutdown("uncaughtException");8});910// Unhandled promise rejections11process.on("unhandledRejection", (reason, promise) => {12 console.error("Unhandled Rejection:", reason);13 logger.error({ reason });14 // Graceful shutdown (Node 15+ crashes by default)15 gracefulShutdown("unhandledRejection");16});
Best practices: