Catch Unhandled Promise Rejections in Node.js Cron Jobs

Node.js

The Problem

An unhandled promise rejection in Node.js can terminate the process (Node 15+) or produce a warning that goes unnoticed. Either way, your cron job stops working.

The Solution

Add a global unhandledRejection handler that reports failures to CronRabbit, then use proper try/catch in all async job handlers.

The Danger of Unhandled Rejections

Since Node.js 15, unhandled promise rejections terminate the process by default. In earlier versions, they produce a warning but the job may continue in a broken state.

Defense in Depth

Always use try/catch in async handlers. Add a global handler as a safety net. Pair with CronRabbit for external detection.

Code Examples

Global rejection handler with monitoring

JavaScript
process.on("unhandledRejection", async (reason) => {
  console.error("Unhandled rejection:", reason);
  try {
    await fetch("https://ping.cronrabbit.com/your-id/fail");
  } catch {} // Best effort
  process.exit(1);
});

process.on("uncaughtException", async (err) => {
  console.error("Uncaught exception:", err);
  try {
    await fetch("https://ping.cronrabbit.com/your-id/fail");
  } catch {}
  process.exit(1);
});

Ready to Monitor Your Cron Jobs?

Create your free account and add your first monitor in under 5 minutes. No credit card required.