@zedtech/zedkit-server
Tell a Postgres conflict from a bug
Extract the Postgres error code (also from an ORM-wrapped `cause`) so unique violations map to 409, not 500.
At a glance
- Who: Developers mapping Postgres driver errors to HTTP responses.
- What: Extract the Postgres error code (also from an ORM-wrapped
cause) so unique violations map to 409, not 500. - Use when: Around writes where a conflict is an expected outcome (signup, idempotent creates).
- Avoid when: For query building or migrations — use your ORM or migration tool instead.
- See also: Return uniform error bodies
Install
pnpm add @zedtech/zedkit-serverLatest: 1.3.1 (via changesets).
Example
import { pgErrorCode } from "@zedtech/zedkit-server";
try {
await db.insert(users).values(row);
} catch (error) {
if (pgErrorCode(error) === "23505") return conflict();
throw error;
}Wraps: (none)