mirror of
https://github.com/bitbaum/petvity.git
synced 2026-12-04 08:07:10 +01:00
Caught by exercising the live endpoint as an admin rather than trusting the unit test: creating a second post with an existing slug answered 500. Drizzle wraps driver errors, so the pg error carrying `code: 23505` is the `cause` of what the route catches, not the error itself. Reading only the top-level `code` looks correct in a test — where the error is hand-built with the code right on it — and is wrong against a real database. The test passed and production returned 500 for a mistake the author can fix in two seconds. isUniqueViolation now walks the cause chain, bounded, with a guard against a self-referencing chain hanging the request. Both tests now use the wrapped shape production actually throws, which is the point: the old test asserted the shape I assumed, not the shape reality produces. Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
43 lines
1.6 KiB
TypeScript
43 lines
1.6 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
|
import { isUniqueViolation } from "./errors";
|
|
|
|
describe("isUniqueViolation", () => {
|
|
it("recognises a bare driver error", () => {
|
|
expect(isUniqueViolation(Object.assign(new Error("dup"), { code: "23505" }))).toBe(true);
|
|
});
|
|
|
|
// The shape production actually throws. Drizzle wraps the driver error, so a
|
|
// check that only reads the top-level `code` passes every unit test and still
|
|
// answers 500 on a duplicate — which is exactly what happened live.
|
|
it("recognises the error Drizzle wraps around it", () => {
|
|
const wrapped = Object.assign(new Error("Failed query"), {
|
|
cause: Object.assign(new Error("duplicate key"), { code: "23505" }),
|
|
});
|
|
expect(isUniqueViolation(wrapped)).toBe(true);
|
|
});
|
|
|
|
it("recognises it through more than one layer of wrapping", () => {
|
|
const inner = Object.assign(new Error("duplicate key"), { code: "23505" });
|
|
const wrapped = { cause: { cause: inner } };
|
|
expect(isUniqueViolation(wrapped)).toBe(true);
|
|
});
|
|
|
|
it.each([
|
|
["a different postgres error", Object.assign(new Error("fk"), { code: "23503" })],
|
|
["a wrapped different error", { cause: { code: "23503" } }],
|
|
["a plain error", new Error("boom")],
|
|
["null", null],
|
|
["undefined", undefined],
|
|
["a string", "23505"],
|
|
])("does not claim %s is a unique violation", (_label, value) => {
|
|
expect(isUniqueViolation(value)).toBe(false);
|
|
});
|
|
|
|
it("terminates on a self-referencing cause chain", () => {
|
|
// A cycle must not hang the request thread.
|
|
const cyclic: { cause?: unknown } = {};
|
|
cyclic.cause = cyclic;
|
|
expect(isUniqueViolation(cyclic)).toBe(false);
|
|
});
|
|
});
|