66 lines
2.4 KiB
TypeScript
66 lines
2.4 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
|
import { formatErrorForUser } from "../../src/error-formatter.js";
|
|
|
|
describe("formatErrorForUser", () => {
|
|
it("includes the error name for standard Error instances", () => {
|
|
const err = new TypeError("something went wrong");
|
|
const result = formatErrorForUser(err);
|
|
expect(result).toContain("TypeError");
|
|
expect(result).toContain("something went wrong");
|
|
});
|
|
|
|
it("includes custom error names", () => {
|
|
const err = new Error("bad auth");
|
|
err.name = "AuthenticationError";
|
|
const result = formatErrorForUser(err);
|
|
expect(result).toContain("AuthenticationError");
|
|
expect(result).toContain("bad auth");
|
|
});
|
|
|
|
it("excludes stack traces from error messages", () => {
|
|
const err = new Error("fail\n at Object.<anonymous> (/home/user/app.js:10:5)\n at Module._compile (node:internal/modules/cjs/loader:1234:14)");
|
|
const result = formatErrorForUser(err);
|
|
expect(result).not.toMatch(/\bat\s+/);
|
|
expect(result).not.toContain("app.js");
|
|
});
|
|
|
|
it("excludes API keys (sk-... pattern)", () => {
|
|
const err = new Error("Auth failed with key sk-ant-api03-abcdefghijklmnop");
|
|
const result = formatErrorForUser(err);
|
|
expect(result).not.toContain("sk-ant-api03");
|
|
expect(result).toContain("[REDACTED]");
|
|
});
|
|
|
|
it("excludes Unix file paths", () => {
|
|
const err = new Error("File not found: /home/user/projects/app/config.ts");
|
|
const result = formatErrorForUser(err);
|
|
expect(result).not.toContain("/home/user");
|
|
expect(result).toContain("[REDACTED_PATH]");
|
|
});
|
|
|
|
it("excludes Windows file paths", () => {
|
|
const err = new Error("Cannot read C:\\Users\\admin\\secrets.txt");
|
|
const result = formatErrorForUser(err);
|
|
expect(result).not.toContain("C:\\Users");
|
|
expect(result).toContain("[REDACTED_PATH]");
|
|
});
|
|
|
|
it("handles string errors", () => {
|
|
const result = formatErrorForUser("something broke");
|
|
expect(result).toBe("Error: something broke");
|
|
});
|
|
|
|
it("handles non-Error, non-string values", () => {
|
|
expect(formatErrorForUser(42)).toBe("An unknown error occurred");
|
|
expect(formatErrorForUser(null)).toBe("An unknown error occurred");
|
|
expect(formatErrorForUser(undefined)).toBe("An unknown error occurred");
|
|
});
|
|
|
|
it("handles Error with empty message", () => {
|
|
const err = new Error("");
|
|
err.name = "RangeError";
|
|
const result = formatErrorForUser(err);
|
|
expect(result).toContain("RangeError");
|
|
});
|
|
});
|