diff --git a/apps/api/src/lib/reporter.ts b/apps/api/src/lib/reporter.ts index 62f2ad1..cc229fa 100644 --- a/apps/api/src/lib/reporter.ts +++ b/apps/api/src/lib/reporter.ts @@ -12,18 +12,19 @@ import { createHash } from "node:crypto"; /** * Transient PostgreSQL connection errors during a restart, graceful shutdown, network blip, or pool * exhaustion. Prisma surfaces these as `PrismaClientKnownRequestError` wrapping the raw SQLSTATE - * (57P01/57P03/53300) or its own P1001/P1017. The pool reconnects on the next attempt, so these are not - * code bugs — reporting them only trips the Error Loop with one-off incidents from every code path that - * touches the database during the brief outage. Individual call sites that need to stop a batch or retry - * still check `isPostgresAdminShutdown` / `isPrismaServerUnreachable` (lib/pg.ts); this filter keeps the - * hub quiet for the paths that just let the error propagate to a generic log.error (BIT-32490 and 11 - * sibling clusters, BIT-33548 for 53300). + * (57P01/57P03/53300) or its own P1001/P1017. The realtime LISTEN/NOTIFY path uses `pg` directly and + * surfaces raw Node TCP errors (ECONNREFUSED) when Postgres is not listening at all. The pool reconnects + * on the next attempt, so these are not code bugs — reporting them only trips the Error Loop with one-off + * incidents from every code path that touches the database during the brief outage. Individual call sites + * that need to stop a batch or retry still check `isPostgresAdminShutdown` / `isPrismaServerUnreachable` + * (lib/pg.ts); this filter keeps the hub quiet for the paths that just let the error propagate to a + * generic log.error (BIT-32490 and 11 sibling clusters, BIT-33548 for 53300, BIT-33524 for ECONNREFUSED). */ function isTransientDatabaseError(err: unknown): boolean { if (!(err instanceof Error)) return false; const code = (err as { code?: unknown }).code; - if (code === "P1001" || code === "P1017") return true; - return /57P01|57P03|53300|remaining connection slots are reserved|the database system is shutting down|the database system is starting up|Can't reach database server|Server has closed the connection/i.test(err.message); + if (code === "P1001" || code === "P1017" || code === "ECONNREFUSED") return true; + return /57P01|57P03|53300|remaining connection slots are reserved|the database system is shutting down|the database system is starting up|Can't reach database server|Server has closed the connection|ECONNREFUSED/i.test(err.message); } export type ReportLevel = "critical" | "error" | "warning" | "info"; diff --git a/apps/api/test/reporter.test.ts b/apps/api/test/reporter.test.ts index 1f8959c..0e8e7c0 100644 --- a/apps/api/test/reporter.test.ts +++ b/apps/api/test/reporter.test.ts @@ -91,7 +91,7 @@ describe("errors to errors.bitreport.pt", () => { expect(events[0]!.context).toMatchObject({ log_message: "archive: image not indexed", fileId: "f2" }); }); - it("does not report transient PostgreSQL shutdown/restart errors (57P01/57P03/P1001/P1017)", async () => { + it("does not report transient PostgreSQL shutdown/restart/unreachable errors (57P01/57P03/53300/P1001/P1017/ECONNREFUSED)", async () => { const hub = fakeHub(); const reporter = reporterWith(hub.fetchImpl); const sink = new Writable({ write: (_c, _e, cb) => cb() }); @@ -106,7 +106,9 @@ describe("errors to errors.bitreport.pt", () => { log.error({ err: mk("Can't reach database server at localhost:5432", "P1001") }, "db unreachable"); log.error({ err: mk("Server has closed the connection.", "P1017") }, "connection closed"); log.error({ err: mk("Raw query failed. Code: `53300`. Message: `remaining connection slots are reserved for roles with the SUPERUSER attribute`", "P2010") }, "health: query failed"); + log.error({ err: mk("connect ECONNREFUSED 127.0.0.1:5432", "ECONNREFUSED") }, "realtime error"); reporter.reportError(mk("Database error. Code: 57P03. Message: the database system is shutting down"), "worker"); + reporter.reportError(mk("connect ECONNREFUSED 127.0.0.1:5432", "ECONNREFUSED"), "api"); log.error({ err: new Error("render crashed") }, "real error"); await reporter.flush(); const events = hub.sent.flatMap((s) => s.events);