refactor(runtime): centralize safe JSON parsing and preserve error causes (#629)
* refactor(domain): consolidate canonical ordering and digest helpers (#624) * refactor(domain): consolidate canonical ordering and digest helpers Single owner for code-point ordering (domain/canonicalOrdering.ts) and canonical JSON digests (domain/comparisonSemantics.ts). Delete all local compareCodePoints/compareText/digestCanonical duplicates and migrate every importer. No re-export shims. * fix(ci): allow slower real browser startup * chore(config): remove dead eslint, dedupe scripts, cacheable turbo tasks Delete eslint.config.js (oxlint is the linter), drop lint:dead alias, pin smol-toml, fold jscpd/scan:todos into check:pr, make test:run cacheable, align oxlint ignores. * fix(config): hash all test inputs for cached runs * docs: fix architecture diagram and pretty-print tool catalog Drop dangling REPLAY edges, add dotnet/reference/error/evaluation nodes. Emit 2-space JSON from the catalog generator and regenerate. * test: extract shared fixtures, drop knip carve-outs Dedupe android/apple session tests, bundler adapter headers, and reconstruction helpers into tests/support fixtures. Delete unused type aliases and the knip ignoreIssues block. Lower jscpd threshold 2.7 to 2.5. * refactor(app): split Setup and ProcessOwnership modules Extract SetupTypes/SetupHost from Setup.ts and move observation helpers into ProcessOwnershipObservation/ProcessTree. Migrate all importers including CLI commands. No re-export shims. * refactor(runtime): centralize safe JSON parsing and preserve error causes Add domain/safeJson.safeParseJson and migrate all raw JSON.parse call sites. Name catch causes and preserve failure reasons instead of swallowing; annotate genuine best-effort cleanup. * fix(runtime): retain JSON parse error causes
This commit is contained in:
+25
-17
@@ -2,6 +2,7 @@ import { readFile } from "node:fs/promises";
|
||||
|
||||
import { AnalysisInputError, projectAnalysisError } from "./domain/errors.js";
|
||||
import type { JsonValue } from "./domain/jsonValue.js";
|
||||
import { safeParseJson } from "./domain/safeJson.js";
|
||||
|
||||
/** Parse inline JSON or one local JSON file for a CLI workflow. */
|
||||
export const parseCliJsonInput = async (
|
||||
@@ -16,27 +17,33 @@ export const parseCliJsonInput = async (
|
||||
if (["{", "["].includes(value.trimStart()[0] ?? ""))
|
||||
return { ok: false, error: inputError(operation) };
|
||||
try {
|
||||
const parsed = parseJson(await readFile(value));
|
||||
return parsed === undefined
|
||||
? jsonFileError(value, operation, "invalid-json")
|
||||
: { ok: true, value: parsed };
|
||||
} catch {
|
||||
return jsonFileError(value, operation, "read-failed");
|
||||
const decoded = safeParseJson(await readFile(value, "utf8"));
|
||||
return decoded.ok
|
||||
? { ok: true, value: decoded.value }
|
||||
: jsonFileError(value, operation, "invalid-json");
|
||||
} catch (cause: unknown) {
|
||||
return jsonFileError(value, operation, "read-failed", cause);
|
||||
}
|
||||
};
|
||||
|
||||
const parseJson = (value: string | Uint8Array): unknown => {
|
||||
try {
|
||||
return JSON.parse(
|
||||
typeof value === "string"
|
||||
? value
|
||||
: new TextDecoder("utf-8", { fatal: true, ignoreBOM: true }).decode(
|
||||
value,
|
||||
),
|
||||
);
|
||||
} catch {
|
||||
return undefined;
|
||||
let text: string;
|
||||
if (typeof value === "string") {
|
||||
text = value;
|
||||
} else {
|
||||
try {
|
||||
text = new TextDecoder("utf-8", {
|
||||
fatal: true,
|
||||
ignoreBOM: true,
|
||||
}).decode(value);
|
||||
} catch (cause: unknown) {
|
||||
// Decoding failure means the bytes are not valid UTF-8 JSON input.
|
||||
void cause;
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
const parsed = safeParseJson(text);
|
||||
return parsed.ok ? parsed.value : undefined;
|
||||
};
|
||||
|
||||
const inputError = (operation: string): JsonValue => ({
|
||||
@@ -52,6 +59,7 @@ const jsonFileError = (
|
||||
path: string | undefined,
|
||||
operation: string,
|
||||
reason: "invalid-json" | "read-failed",
|
||||
cause?: unknown,
|
||||
) => ({
|
||||
ok: false as const,
|
||||
error: {
|
||||
@@ -59,7 +67,7 @@ const jsonFileError = (
|
||||
...projectAnalysisError(
|
||||
new AnalysisInputError(
|
||||
operation,
|
||||
undefined,
|
||||
cause === undefined ? undefined : { cause },
|
||||
reason === "invalid-json"
|
||||
? [{ path: [], reason: "invalid_format", expected: "JSON" }]
|
||||
: [],
|
||||
|
||||
Reference in New Issue
Block a user