38 lines
1.5 KiB
TypeScript
38 lines
1.5 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
|
|
import { toSafeErrorLog } from "./safeErrorLog.server";
|
|
|
|
describe("toSafeErrorLog", () => {
|
|
it("keeps only a bounded error identity", () => {
|
|
const error = Object.assign(new TypeError("postgres://user:secret@example.test/customer@example.test"), { code: "P2002" });
|
|
|
|
expect(toSafeErrorLog(error)).toEqual({ errorName: "TypeError", errorCode: "P2002" });
|
|
expect(JSON.stringify(toSafeErrorLog(error))).not.toContain("secret");
|
|
expect(JSON.stringify(toSafeErrorLog(error))).not.toContain("customer@example.test");
|
|
});
|
|
|
|
it("does not serialize arbitrary thrown values or unbounded codes", () => {
|
|
expect(toSafeErrorLog("token=secret")).toEqual({ errorName: "UnknownError" });
|
|
expect(toSafeErrorLog(Object.assign(new Error("secret"), { code: "x".repeat(65) }))).toEqual({ errorName: "Error" });
|
|
expect(toSafeErrorLog(Object.assign(new Error("secret"), { name: "token=secret", code: "lowercase-secret" }))).toEqual({
|
|
errorName: "Error",
|
|
});
|
|
});
|
|
|
|
it("rejects secret-shaped uppercase names and codes instead of treating their shape as safe", () => {
|
|
const error = Object.assign(new Error("hidden"), {
|
|
name: "SECRET_API_KEY",
|
|
code: "PASSWORD:SUPERSECRET",
|
|
});
|
|
|
|
expect(toSafeErrorLog(error)).toEqual({ errorName: "Error" });
|
|
});
|
|
|
|
it("retains only explicitly supported operational codes", () => {
|
|
expect(toSafeErrorLog(Object.assign(new Error("offline"), { code: "ECONNREFUSED" }))).toEqual({
|
|
errorName: "Error",
|
|
errorCode: "ECONNREFUSED",
|
|
});
|
|
});
|
|
});
|