Files
rea/src/reference/ReferenceSourceReaderErrors.ts
T
morluto 3967cfa43e fix(reference): distinguish root access failures from invalid input
Share root filesystem failure classification between import preflight and traversal. Missing roots remain invalid selections; permission and I/O errors retain their reason and reach the CLI as execution failures. Preserve cancellation priority and expose unexpected exceptions.

Validation: compiled CLI permission regression, direct reader error details, import/traversal and unreadable-directory boundaries passed. Windows-only cases were skipped on macOS; static, architecture and documentation checks passed.
2026-10-11 03:53:50 +08:00

88 lines
2.2 KiB
TypeScript

import {
type ReferenceSourceEntry,
type ReferenceSourceFailureCode,
type 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",
"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 }),
});