Files
HealthLog/src/lib/prisma-errors.ts
T
Marc-André BombeckandClaude Opus 4.8 ba4349eda0 refactor(db): extract the P2002 predicate into one shared helper
The Prisma unique-constraint (P2002) narrowing predicate had been
hand-copied into five call sites. Fold them into a single isP2002 in
src/lib/prisma-errors.ts and import it everywhere (the two step-drain
sites keep their local isUniqueConstraintViolation name via an import
alias). Behaviour-identical; one place to chase a future P2002-semantics
change instead of five.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-04 13:26:07 +02:00

25 lines
899 B
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 the P2002 unique-constraint code. */
export function isP2002(err: unknown): boolean {
return (
typeof err === "object" &&
err !== null &&
"code" in err &&
(err as { code: unknown }).code === "P2002"
);
}