|
| 1 | +import { logger } from './logger'; |
| 2 | +import SentryLogger from '../sentryLogger'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Registers process-level handlers for errors that escape application code. |
| 6 | + * |
| 7 | + * Why this exists: the API runs behind a Postgres connection pooler |
| 8 | + * (DigitalOcean managed Postgres / PgBouncer). When the pooler or database has |
| 9 | + * a transient problem, in-flight DB queries reject. Without an |
| 10 | + * `unhandledRejection` listener, Node (>= 15) terminates the whole process on |
| 11 | + * the first such rejection, taking the entire API down for a momentary DB blip. |
| 12 | + * |
| 13 | + * Behaviour: |
| 14 | + * - unhandledRejection: log + report to Sentry, then KEEP the process alive. |
| 15 | + * A single rejected promise (often a transient DB error) must not tear down |
| 16 | + * the server; it should keep serving and recover once the DB is reachable. |
| 17 | + * - uncaughtException: log + report to Sentry, then EXIT. The process state is |
| 18 | + * undefined after an uncaught exception, so we let the container restart |
| 19 | + * policy (`restart: always`) recreate a clean process. |
| 20 | + */ |
| 21 | +let handlersRegistered = false; |
| 22 | + |
| 23 | +export function registerGlobalErrorHandlers(): void { |
| 24 | + // Idempotent: never attach the listeners more than once. |
| 25 | + if (handlersRegistered) { |
| 26 | + return; |
| 27 | + } |
| 28 | + handlersRegistered = true; |
| 29 | + |
| 30 | + process.on('unhandledRejection', (reason: unknown) => { |
| 31 | + logger.error('unhandledRejection - process kept alive', reason); |
| 32 | + captureToSentry(reason); |
| 33 | + }); |
| 34 | + |
| 35 | + process.on('uncaughtException', (error: Error) => { |
| 36 | + logger.fatal('uncaughtException - exiting for a clean restart', error); |
| 37 | + captureToSentry(error); |
| 38 | + flushSentryAndExit(); |
| 39 | + }); |
| 40 | +} |
| 41 | + |
| 42 | +function captureToSentry(error: unknown): void { |
| 43 | + try { |
| 44 | + SentryLogger.captureException( |
| 45 | + error instanceof Error ? error : new Error(String(error)), |
| 46 | + ); |
| 47 | + } catch { |
| 48 | + // Never let error reporting throw from inside an error handler. |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * Flushes pending Sentry events (best-effort, max 2s) and then exits with a |
| 54 | + * non-zero code so the container restart policy (`restart: always`) recreates a |
| 55 | + * clean process. Used by the uncaughtException handler above and by the |
| 56 | + * bootstrap() startup-failure path. |
| 57 | + */ |
| 58 | +export function flushSentryAndExit(): void { |
| 59 | + // A hard fallback guarantees the process exits even if flushing stalls. |
| 60 | + const forceExit = setTimeout(() => process.exit(1), 3000); |
| 61 | + void SentryLogger.close(2000) |
| 62 | + .catch(() => undefined) |
| 63 | + .then(() => { |
| 64 | + clearTimeout(forceExit); |
| 65 | + process.exit(1); |
| 66 | + }); |
| 67 | +} |
0 commit comments