/** * @file tests/fixes_v1_2.test.js * @description Regression suite for the v1.2.0 bug fixes and additions: * 1. Coerced errors keep the original stack trace and cause * 2. AppError status validation (NaN, Infinity, strings, floats) * 3. Cross-copy instanceof (dual CJS/ESM package hazard) and foreign AppError shapes * 4. Zod, Joi, Prisma, Mongoose DocumentNotFound coercion * 5. Third-party status mapping (new codes, numeric strings, http-errors `expose`) * 6. sanitizeDetails (shared refs, Map/Set, Error, Date, depth, hostile getters) * 7. toJSON safety, null options, async coercers, request ID validation * 8. toHttpResponse * 9. Logger: bad level, level change safety, serializer merge, base fields, frozen errors * 10. Express: null logger, winston style, includeStackInLog, numeric req.id, * generateRequestId, onError, headersSent logging, not-found handler, asyncHandler */ "use strict"; const http = require("node:http"); const { Writable } = require("node:stream"); const express = require("express"); const fk = require("../dist/cjs/index.js"); const { createExpressErrorHandler, createNotFoundHandler, asyncHandler, } = require("../dist/cjs/express.js"); let passed = 0; let failed = 0; function assert(condition, name, details = "") { if (condition) { console.log(` βœ… ${name}`); passed++; } else { console.error(` ❌ FAILED: ${name}${details ? ` (${details})` : ""}`); failed++; } } function captureLogger(options = {}) { const lines = []; const dest = new Writable({ write(chunk, _enc, cb) { for (const line of chunk.toString().split("\n")) { if (line.trim()) lines.push(JSON.parse(line)); } cb(); }, }); return { logger: fk.createLogger(options, dest), lines }; } function recorder() { const calls = []; return { calls, pino: { child() { return this; }, warn: (...a) => calls.push({ level: "warn", args: a }), error: (...a) => calls.push({ level: "error", args: a }), }, winston: { transports: [], format: {}, child() { return this; }, warn: (...a) => calls.push({ level: "warn", args: a }), error: (...a) => calls.push({ level: "error", args: a }), }, }; } async function hit(app, path, headers = {}) { const server = app.listen(0); await new Promise((r) => server.once("listening", r)); const { port } = server.address(); try { return await new Promise((resolve, reject) => { const req = http.request(`http://127.0.0.1:${port}${path}`, { headers }, (res) => { let raw = ""; // A response cut off mid-stream emits 'error' / 'aborted' instead of 'end'. res.on("error", reject); res.on("aborted", () => reject(new Error("aborted"))); res.on("data", (c) => (raw += c)); res.on("end", () => { let body = raw; try { body = JSON.parse(raw); } catch { /* keep text */ } resolve({ status: res.statusCode, headers: res.headers, body }); }); }); req.on("error", reject); req.end(); }); } finally { server.close(); } } async function run() { console.log("\nπŸ“¦ v1.2.0 Bug-Fix Regression Suite"); console.log("────────────────────────────────────────────────────────────"); // ── 1. Stack + cause preservation ───────────────────────────────────────── console.log("\nπŸ“‹ [1] Original stack trace and cause are preserved"); { function originalThrowSite() { return null.boom; } let native; try { originalThrowSite(); } catch (e) { native = e; } const coerced = fk.coerceToAppError(native); assert(coerced.stack.includes("originalThrowSite"), "Native error stack points at the real throw site"); assert(coerced.stack.startsWith("TypeError"), "Stack keeps the original error type"); const root = new Error("socket closed"); const wrapped = new Error("query failed", { cause: root }); assert(fk.coerceToAppError(wrapped).cause === root, "Original cause is carried over"); const own = new fk.ServiceUnavailableError("db down", null, { cause: root }); assert(own.cause === root, "AppError subclasses accept { cause }"); assert(!Object.keys(own).includes("cause"), "cause is non-enumerable"); assert(fk.toErrorResponse(own).error.details === null, "cause never reaches the response"); const mongo = Object.assign(new Error("E11000 duplicate key"), { code: 11000, keyValue: { email: "a@b.c" } }); assert(fk.coerceToAppError(mongo).stack === mongo.stack, "Duplicate-key coercion keeps the Mongo stack"); } // ── 2. AppError status validation ───────────────────────────────────────── console.log("\nπŸ“‹ [2] Invalid AppError status codes become 500 (non-operational)"); for (const bad of [NaN, Infinity, -Infinity, "404", 200, 99, 600]) { class BadStatus extends fk.AppError { constructor() { super("bad status", bad, "SOME_CODE", null, true); } } const out = fk.coerceToAppError(new BadStatus()); assert(out.statusCode === 500 && out.isOperational === false, `status ${String(bad)} β†’ 500 non-operational`); const res = fk.toErrorResponse(new BadStatus(), { isProduction: true }); assert(res.error.message !== "bad status", `status ${String(bad)} message masked in production`); } { class FloatStatus extends fk.AppError { constructor() { super("float", 404.9, "NOT_FOUND"); } } assert(fk.coerceToAppError(new FloatStatus()).statusCode === 404, "Float status 404.9 rounds down to 404"); } // ── 3. Cross-copy AppError ──────────────────────────────────────────────── console.log("\nπŸ“‹ [3] Errors from other faultkit copies keep their code"); { const brand = Symbol.for("faultkit.AppError"); const foreign = Object.assign(Object.create(Error.prototype), { name: "ValidationError", message: "bad email", statusCode: 400, errorCode: "VALIDATION_ERROR", details: { email: ["bad"] }, isOperational: true, }); Object.defineProperty(foreign, brand, { value: true }); assert(foreign instanceof fk.AppError, "Branded object passes instanceof AppError"); assert(!(foreign instanceof fk.NotFoundError), "Subclass instanceof keeps prototype semantics"); const legacy = Object.assign(new Error("legacy"), { statusCode: 409, errorCode: "CONFLICT", isOperational: true, details: { id: 1 }, }); const res = fk.toErrorResponse(legacy); assert(res.error.code === "CONFLICT", "Unbranded AppError-shaped error keeps its errorCode"); assert(res.error.details && res.error.details.id === 1, "…and its details"); const hostile = new Proxy( {}, { get() { throw new Error("trap"); }, getPrototypeOf() { throw new Error("trap"); }, } ); let threw = false; try { void (hostile instanceof fk.AppError); } catch { threw = true; } assert(!threw, "instanceof AppError never throws on hostile proxies"); } // ── 4. Library coercion ─────────────────────────────────────────────────── console.log("\nπŸ“‹ [4] Zod, Joi, Prisma and Mongoose coercion"); { const zod = Object.assign(new Error("[...]"), { name: "ZodError", issues: [ { path: ["email"], message: "Required" }, { path: ["address", "zip"], message: "Too short" }, { path: [], message: "Bad object" }, ], }); const z = fk.coerceToAppError(zod); assert(z.statusCode === 400 && z.errorCode === "VALIDATION_ERROR", "ZodError β†’ 400 VALIDATION_ERROR"); assert(z.details.email[0] === "Required", "Zod field errors use { field: [messages] }"); assert(z.details["address.zip"][0] === "Too short", "Nested Zod paths are dot-joined"); assert(z.details._root[0] === "Bad object", "Root-level Zod issues go to _root"); const joi = Object.assign(new Error('"name" is required'), { name: "ValidationError", isJoi: true, details: [{ message: '"name" is required', path: ["name"] }], }); const j = fk.coerceToAppError(joi); assert(j.errorCode === "VALIDATION_ERROR" && j.details.name[0] === '"name" is required', "Joi β†’ VALIDATION_ERROR"); const prisma = (code, meta) => Object.assign(new Error("prisma"), { name: "PrismaClientKnownRequestError", code, meta, clientVersion: "5.0.0" }); const p2002 = fk.coerceToAppError(prisma("P2002", { target: ["email"] })); assert(p2002.statusCode === 409 && p2002.details.fields[0] === "email", "Prisma P2002 β†’ 409 CONFLICT"); assert(fk.coerceToAppError(prisma("P2025")).statusCode === 404, "Prisma P2025 β†’ 404 NOT_FOUND"); assert(fk.coerceToAppError(prisma("P2003", { field_name: "userId" })).statusCode === 409, "Prisma P2003 β†’ 409"); assert(fk.coerceToAppError(prisma("P2000", { column_name: "bio" })).statusCode === 400, "Prisma P2000 β†’ 400"); assert(fk.coerceToAppError(prisma("P1001")).statusCode === 500, "Unknown Prisma codes β†’ 500"); const docNotFound = Object.assign(new Error("No document found"), { name: "DocumentNotFoundError" }); assert(fk.coerceToAppError(docNotFound).statusCode === 404, "Mongoose DocumentNotFoundError β†’ 404"); } // ── 5. Third-party status mapping ───────────────────────────────────────── console.log("\nπŸ“‹ [5] Third-party HTTP status mapping"); { const cases = [ [402, "PAYMENT_REQUIRED"], [405, "METHOD_NOT_ALLOWED"], [408, "REQUEST_TIMEOUT"], [410, "GONE"], [413, "PAYLOAD_TOO_LARGE"], [415, "UNSUPPORTED_MEDIA_TYPE"], [418, "BAD_REQUEST"], [501, "NOT_IMPLEMENTED"], [502, "BAD_GATEWAY"], [507, "INTERNAL_SERVER_ERROR"], ]; for (const [status, code] of cases) { const e = Object.assign(new Error("lib"), { status }); assert(fk.coerceToAppError(e).errorCode === code, `status ${status} β†’ ${code}`); } assert(fk.coerceToAppError({ statusCode: "404", message: "x" }).statusCode === 404, "Numeric string status accepted"); const hidden = Object.assign(new Error("secret internals"), { status: 400, expose: false }); assert(fk.coerceToAppError(hidden).isOperational === false, "http-errors expose:false β†’ non-operational"); assert(fk.errorCodeFromStatus(404) === "NOT_FOUND", "errorCodeFromStatus is exported"); assert(fk.toErrorStatus(200) === undefined && fk.toErrorStatus("503") === 503, "toErrorStatus validates"); } // ── 6. sanitizeDetails ──────────────────────────────────────────────────── console.log("\nπŸ“‹ [6] sanitizeDetails"); { const shared = { v: 1 }; const out = fk.sanitizeDetails({ a: shared, b: shared }); assert(out.b && out.b.v === 1, "Shared (non-circular) references are not marked [Circular]"); const circ = { name: "loop" }; circ.self = circ; assert(fk.sanitizeDetails(circ).self === "[Circular]", "Real cycles become [Circular]"); const rich = fk.sanitizeDetails({ big: 10n, when: new Date("2026-01-01T00:00:00.000Z"), map: new Map([["k", 1]]), set: new Set([1, 2]), err: new Error("inner"), nan: NaN, fn: () => 1, arr: [undefined, 1], }); assert(rich.big === "10", "BigInt β†’ string"); assert(rich.when === "2026-01-01T00:00:00.000Z", "Date β†’ ISO string"); assert(rich.map.k === 1, "Map β†’ object"); assert(Array.isArray(rich.set) && rich.set.length === 2, "Set β†’ array"); assert(rich.err.message === "inner" && rich.err.stack === undefined, "Error β†’ { name, message } without stack"); assert(rich.nan === null, "NaN β†’ null"); assert(!("fn" in rich), "Functions dropped"); assert(rich.arr[0] === null, "undefined in arrays β†’ null"); const hostile = {}; Object.defineProperty(hostile, "x", { enumerable: true, get() { throw new Error("trap"); }, }); assert(fk.sanitizeDetails({ ok: 1, hostile }).hostile.x === "[Unreadable]", "Throwing getters are contained"); let deep = {}; const top = deep; for (let i = 0; i < 50; i++) deep = deep.n = {}; assert(JSON.stringify(fk.sanitizeDetails(top)).includes("[Max Depth Exceeded]"), "Depth is limited"); } // ── 7. toJSON, options, async coercer, request IDs ──────────────────────── console.log("\nπŸ“‹ [7] toJSON, null options, async coercers and request IDs"); { let threw = false; try { JSON.stringify(new fk.ValidationError({ n: 10n })); } catch { threw = true; } assert(!threw, "JSON.stringify(AppError) with BigInt details does not throw"); assert(new fk.NotFoundError().toJSON({ requestId: "req-1" }).requestId === "req-1", "toJSON({ requestId }) works"); assert(new fk.NotFoundError().toJSON("req-2").requestId === "req-2", "toJSON(string) still works"); let nullThrew = false; try { fk.toErrorResponse(new Error("x"), null); fk.toHttpResponse(new Error("x"), null); fk.createLogger(null, new Writable({ write: (_c, _e, cb) => cb() })); createExpressErrorHandler(null); createNotFoundHandler(null); } catch { nullThrew = true; } assert(!nullThrew, "null options never crash"); const asyncRes = fk.toErrorResponse(new fk.NotFoundError("nf"), { errorCoercer: async () => { throw new Error("async boom"); }, }); assert(asyncRes.error.code === "NOT_FOUND", "Async coercer result is ignored (original error kept)"); assert(fk.normalizeRequestId(" abc-123 ") === "abc-123", "Request ID trimmed"); assert(fk.normalizeRequestId(42) === "42", "Numeric request ID β†’ string"); assert(fk.normalizeRequestId("a".repeat(257)) === undefined, "Oversized request ID rejected"); assert(fk.normalizeRequestId("x\r\nSet-Cookie: a=b") === undefined, "CRLF request ID rejected"); assert(fk.normalizeRequestId("