Something went wrong. Try again.
🌿 Collaborative wiki on ATProto forked from juprodh.me/lichen.wiki
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283import { describe, expect, test } from "bun:test";import { AppError, ForbiddenError, formatError, isUniqueConstraintError, NotFoundError, PdsWriteError, ValidationError,} from "../../src/lib/errors.ts";
describe("error classes", () => { test("each subclass has the correct statusCode", () => { expect(new NotFoundError().statusCode).toBe(404); expect(new ForbiddenError().statusCode).toBe(403); expect(new PdsWriteError("fail").statusCode).toBe(502); expect(new ValidationError("bad").statusCode).toBe(400); });
test("all subclasses extend AppError and Error", () => { for (const err of [ new NotFoundError(), new ForbiddenError(), new PdsWriteError("fail"), new ValidationError("bad"), ]) { expect(err).toBeInstanceOf(AppError); expect(err).toBeInstanceOf(Error); } });
test("default messages are set", () => { expect(new NotFoundError().message).toBe("Not found"); expect(new ForbiddenError().message).toBe("Forbidden"); });
test("custom messages are preserved", () => { expect(new NotFoundError("wiki gone").message).toBe("wiki gone"); expect(new PdsWriteError("PDS down").message).toBe("PDS down"); });});
describe("formatError", () => { test("extracts message from Error instances", () => { expect(formatError(new Error("boom"))).toBe("boom"); });
test("converts non-Error values to string", () => { expect(formatError("string error")).toBe("string error"); expect(formatError(42)).toBe("42"); expect(formatError(null)).toBe("null"); });});
describe("isUniqueConstraintError", () => { test("matches SQLITE_CONSTRAINT_PRIMARYKEY", () => { const err = Object.assign(new Error("PK"), { code: "SQLITE_CONSTRAINT_PRIMARYKEY", }); expect(isUniqueConstraintError(err)).toBe(true); });
test("matches SQLITE_CONSTRAINT_UNIQUE", () => { const err = Object.assign(new Error("UQ"), { code: "SQLITE_CONSTRAINT_UNIQUE", }); expect(isUniqueConstraintError(err)).toBe(true); });
test("rejects unrelated errors and non-Error values", () => { expect(isUniqueConstraintError(new Error("plain"))).toBe(false); expect( isUniqueConstraintError( Object.assign(new Error("fk"), { code: "SQLITE_CONSTRAINT_FOREIGNKEY", }), ), ).toBe(false); expect(isUniqueConstraintError(null)).toBe(false); expect(isUniqueConstraintError("nope")).toBe(false); });});