Files
HealthLog/src/lib/prisma-errors.ts
T
Marc-André Bombeck 336ce5aef3 fix(api): narrow swallowed database errors and redact the incident URL
The incident forwarder stripped the query string but not path-segment
secrets from the reported URL; route it through the same redaction the
message and stack already use. Several delete-and-swallow paths caught
every error as the expected unique/absent-row case, masking real
failures on security-notification, logout, and integration-disconnect
paths and the Telegram reply-consume guard; narrow each to the specific
Prisma code and let real failures surface.
2026-07-01 23:45:08 +02:00

40 lines
1.4 KiB
TypeScript

/**
* Shared Prisma error narrowing helpers.
*
* The P2002 (unique-constraint violation) predicate was hand-copied into
* five call sites (rollup persist, intake-slot dedup, slot upsert, legacy
* step consolidation, dense-intraday retention). v1.11.2 folds them into
* this single helper so a future change to the P2002 semantics has one
* place to live instead of five.
*
* Deliberately structural (`typeof === "object"` + `"code" in err` +
* `code === "P2002"`) rather than `instanceof Prisma.PrismaClientKnownRequestError`
* so it survives client-bundling quirks while staying specific to the
* unique-constraint code.
*/
/** Narrow a thrown Prisma error to a specific known-request code. */
function hasPrismaCode(err: unknown, code: string): boolean {
return (
typeof err === "object" &&
err !== null &&
"code" in err &&
(err as { code: unknown }).code === code
);
}
/** Narrow a thrown Prisma error to the P2002 unique-constraint code. */
export function isP2002(err: unknown): boolean {
return hasPrismaCode(err, "P2002");
}
/**
* Narrow a thrown Prisma error to the P2025 "record not found" code — the
* error `delete` / `update` raise when the target row no longer exists. Lets
* an idempotent delete treat "already gone" as success while still surfacing a
* genuine failure (a real DB fault raises a different code and rethrows).
*/
export function isP2025(err: unknown): boolean {
return hasPrismaCode(err, "P2025");
}