Node.js › Error Handling

What is the difference between operational errors and programmer errors in Node.js, and why does this distinction matter?

Beginner

Answer

Operational errors are expected, recoverable failures that arise from the runtime environment or external conditions (a failed network request, invalid user input, a database connection drop) -- these should be handled gracefully, typically returning an appropriate error response. Programmer errors are actual bugs in the code (calling a method on undefined, a typo, incorrect logic) that indicate the program is in an unknown, potentially corrupted state -- these generally should NOT be caught and silently continued from, since doing so risks the application continuing in an inconsistent state; the safer response is usually to let the process crash and restart cleanly.
// Operational error: expected, handle gracefully
try {
  const user = await db.users.findById(id);
} catch (err) {
  if (err.code === 'CONNECTION_TIMEOUT') return res.status(503).json({ error: 'Service temporarily unavailable' });
}

// Programmer error: a bug, shouldn't be silently swallowed
// TypeError: Cannot read properties of undefined -- indicates broken code, not a recoverable condition
Real-world example A team's error-handling strategy treats a failed payment-gateway API call (operational) by retrying and showing the user a friendly error message, while a TypeError from accessing a property on an unexpectedly undefined object (programmer error) is allowed to crash the process, triggering an automatic restart and an alert to fix the actual bug.

Common follow-ups: Why is it dangerous to catch and ignore a programmer error like a TypeError rather than letting the process crash?;How do custom error classes help distinguish operational from programmer errors at the point they're caught?