diff --git a/apps/api/src/lib/reporter.ts b/apps/api/src/lib/reporter.ts index f989545..7399ccf 100644 --- a/apps/api/src/lib/reporter.ts +++ b/apps/api/src/lib/reporter.ts @@ -8,6 +8,22 @@ import { createHash } from "node:crypto"; * * Off without ERROR_REPORTING_TOKEN (the project's ingest key on the hub), or with ERROR_REPORTING_DISABLED=1. */ + +/** + * Transient PostgreSQL connection errors during a restart, graceful shutdown, or network blip. Prisma surfaces + * these as `PrismaClientKnownRequestError` wrapping the raw SQLSTATE (57P01/57P03) 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). + */ +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|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); +} export type ReportLevel = "critical" | "error" | "warning" | "info"; export interface ReportEvent { @@ -93,6 +109,7 @@ export class ErrorReporter { } reportError(err: unknown, component: string, context: Record = {}, level: ReportLevel = "error"): void { + if (isTransientDatabaseError(err)) return; const e = err instanceof Error ? err : new Error(typeof err === "string" ? err : JSON.stringify(err)); const code = (e as { code?: unknown }).code; this.report({ level, message: `${e.name}: ${e.message}`, stack: e.stack, component, context: { error_type: e.constructor?.name ?? "Error", ...(code !== undefined && { error_code: String(code) }), ...context } }); diff --git a/apps/api/test/reporter.test.ts b/apps/api/test/reporter.test.ts index ac55b02..aafec08 100644 --- a/apps/api/test/reporter.test.ts +++ b/apps/api/test/reporter.test.ts @@ -90,6 +90,28 @@ 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 () => { + const hub = fakeHub(); + const reporter = reporterWith(hub.fetchImpl); + const sink = new Writable({ write: (_c, _e, cb) => cb() }); + const log = pino({ level: "info", hooks: reporter.logHooks("api") }, sink); + const mk = (msg: string, code?: string) => { + const e = new Error(msg); + if (code) (e as { code?: string }).code = code; + return e; + }; + log.error({ err: mk("Database error. Code: 57P03. Message: the database system is shutting down") }, "sports: query failed"); + log.error({ err: mk("Database error. Code: 57P01. Message: terminating connection due to administrator command") }, "archive: query failed"); + 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"); + reporter.reportError(mk("Database error. Code: 57P03. Message: the database system is shutting down"), "worker"); + log.error({ err: new Error("render crashed") }, "real error"); + await reporter.flush(); + const events = hub.sent.flatMap((s) => s.events); + expect(events).toHaveLength(1); + expect(events[0]!.message).toContain("render crashed"); + }); }); describe("browser errors through the API", () => {