fix(housekeeping): preserve error correlation IDs

This commit is contained in:
Simo committed 2026-08-26 21:09:18 +02:00
1 parent b7edbca043
commit cc6bb9a9a3
2 files changed
+32 -9

No files matched your search

@@ -145,13 +145,34 @@ describe("housekeeping foundation contracts", () => {
}); });
}); });
it("sanitizes unknown errors with the stable internal taxonomy", () => { it("preserves a supplied correlation ID when mapping an unknown error", () => {
expect(mapUnknownError(new Error("database password exposed"))).toEqual({ expect(
code: "INTERNAL", mapUnknownError(new Error("database password exposed"), "corr-unknown"),
messageKey: "errors.housekeeping.internal", ).toEqual({
ok: false,
error: {
code: "INTERNAL",
messageKey: "errors.housekeeping.internal",
},
correlationId: "corr-unknown",
}); });
}); });
it("generates a correlation ID when mapping an unknown error without one", () => {
const result = mapUnknownError(new Error("database password exposed"));
expect(result).toMatchObject({
ok: false,
error: {
code: "INTERNAL",
messageKey: "errors.housekeeping.internal",
},
});
expect(result.correlationId).toMatch(
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,
);
});
it("rejects empty capability requirements", () => { it("rejects empty capability requirements", () => {
expect(() => anyCapability()).toThrow("capability requirement is empty"); expect(() => anyCapability()).toThrow("capability requirement is empty");
expect(() => allCapabilities()).toThrow("capability requirement is empty"); expect(() => allCapabilities()).toThrow("capability requirement is empty");
@@ -1,3 +1,5 @@
import { createCorrelationId } from "../correlation";
export type HousekeepingErrorCode = export type HousekeepingErrorCode =
| "UNAUTHENTICATED" | "UNAUTHENTICATED"
| "FORBIDDEN" | "FORBIDDEN"
@@ -40,9 +42,9 @@ export function fail(
}; };
} }
export function mapUnknownError(_error: unknown): HousekeepingError { export function mapUnknownError(
return { _error: unknown,
code: "INTERNAL", correlationId = createCorrelationId(),
messageKey: "errors.housekeeping.internal", ): HousekeepingResult<never> {
}; return fail("INTERNAL", "errors.housekeeping.internal", correlationId);
} }