Files
rea/src/reference/ReferenceSourceReaderErrors.ts
T
morluto ec271121e4 fix(artifacts): preserve path identity and exclusion applicability
Normalize path facts once at the owning filesystem boundary, preserve caller spelling and unavailable inventory members, and retain typed read failures. Record exclusion entry kinds so only proven directories can establish descendant symlink targets; omitted legacy kinds stay unknown.

Reviewed fixes include #1969, #2642, #2759, and #2994.

Validation: 26 reference graph source tests passed; prior artifact checks passed 275 tests with one Windows-only skip. Updated compiled import workflows are included in final validation after regeneration.
2026-10-14 08:31:16 +08:00

91 lines
2.3 KiB
TypeScript

import type {
ReferenceSourceEntry,
ReferenceSourceFailureCode,
ReferenceSourceReaderError,
} from "./ReferenceSourceReaderTypes.js";
export const failure = (
code: ReferenceSourceReaderError["code"],
message: string,
): ReferenceSourceReaderError => ({
tag: "reference-source-reader",
code,
message,
});
export const cancelled = (): ReferenceSourceReaderError =>
failure("cancelled", "Reference source traversal cancelled");
export const safeSize = (size: bigint): number | undefined => {
const value = Number(size);
return Number.isSafeInteger(value) && value >= 0 ? value : undefined;
};
const FILESYSTEM_ERROR_CODES = new Set([
"EACCES",
"EBADF",
"EEXIST",
"EIO",
"EISDIR",
"ELOOP",
"EMFILE",
// A traversed child can exceed the platform's absolute path limit even
// though the selected root and every component are valid.
"ENAMETOOLONG",
"ENFILE",
"ENOENT",
"ENOTDIR",
"ENOSPC",
"EPERM",
"EROFS",
]);
/** Preserve actionable operating-system detail while leaving invariants visible. */
export const filesystemFailureDetail = (
cause: unknown,
operation: string,
): string | undefined => {
if (!(cause instanceof Error)) return undefined;
const code: unknown = Reflect.get(cause, "code");
return typeof code === "string" && FILESYSTEM_ERROR_CODES.has(code)
? `${operation}: ${cause.message}`
: undefined;
};
/** Preserve root I/O detail while separating bad selections from access failures. */
export const rootFilesystemFailure = (
cause: unknown,
operation: string,
):
| {
readonly code: "invalid-root" | "io";
readonly message: string;
}
| undefined => {
if (!(cause instanceof Error)) return undefined;
const code: unknown = Reflect.get(cause, "code");
if (typeof code !== "string" || !FILESYSTEM_ERROR_CODES.has(code))
return undefined;
return {
code: code === "ENOENT" || code === "ENOTDIR" ? "invalid-root" : "io",
message: `${operation}: ${cause.message}`,
};
};
export const entryFailure = (
...[path, kind, code, message, size]: readonly [
string,
Extract<ReferenceSourceEntry, { status: "failed" }>["kind"],
ReferenceSourceFailureCode,
string,
size?: number,
]
): ReferenceSourceEntry => ({
status: "failed",
kind,
path,
code,
message,
...(size === undefined ? {} : { size }),
});