2026-10-07 19:22:04 +08:00
|
|
|
import type { ManagedParseIssue } from "../domain/managed/managedArtifact.js";
|
2026-07-16 15:56:40 +08:00
|
|
|
|
|
|
|
|
/** Bounded format failure retained as caller-visible managed coverage. */
|
|
|
|
|
export class ManagedReaderFailure extends Error {
|
|
|
|
|
constructor(
|
|
|
|
|
readonly issue: ManagedParseIssue,
|
|
|
|
|
options?: ErrorOptions,
|
|
|
|
|
) {
|
|
|
|
|
super(issue.detail, options);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** Construct a typed managed parse failure at an exact byte location. */
|
|
|
|
|
export const managedFailure = (
|
|
|
|
|
code: ManagedParseIssue["code"],
|
|
|
|
|
scope: string,
|
|
|
|
|
detail: string,
|
|
|
|
|
offset: number | null = null,
|
|
|
|
|
): ManagedReaderFailure =>
|
|
|
|
|
new ManagedReaderFailure({ code, scope, offset, detail });
|
2026-10-09 20:02:52 +08:00
|
|
|
|
|
|
|
|
/** Retain one malformed item's location while allowing independent items to be read. */
|
|
|
|
|
export const readManagedValue = <Value>(
|
|
|
|
|
operation: () => Value,
|
|
|
|
|
issues: ManagedParseIssue[],
|
|
|
|
|
): Value | undefined => {
|
|
|
|
|
try {
|
|
|
|
|
return operation();
|
|
|
|
|
} catch (cause: unknown) {
|
2026-10-11 00:03:19 +08:00
|
|
|
if (
|
|
|
|
|
!(cause instanceof ManagedReaderFailure) ||
|
|
|
|
|
cause.issue.code === "resource-limit"
|
|
|
|
|
)
|
|
|
|
|
throw cause;
|
2026-10-09 20:02:52 +08:00
|
|
|
issues.push(cause.issue);
|
|
|
|
|
return undefined;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** Preserve issue order while collapsing the same malformed producer fact across facets. */
|
|
|
|
|
export const uniqueManagedParseIssues = (
|
|
|
|
|
issues: readonly ManagedParseIssue[],
|
|
|
|
|
): readonly ManagedParseIssue[] => [
|
|
|
|
|
...new Map(issues.map((issue) => [JSON.stringify(issue), issue])).values(),
|
|
|
|
|
];
|
2026-10-13 15:01:23 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Append issue lists one issue at a time. Spreading a list into `push` passes
|
|
|
|
|
* one argument per issue, which overflows the stack on large malformed inputs.
|
|
|
|
|
*/
|
|
|
|
|
export const appendManagedParseIssues = (
|
|
|
|
|
issues: ManagedParseIssue[],
|
|
|
|
|
...found: readonly (readonly ManagedParseIssue[])[]
|
|
|
|
|
): void => {
|
|
|
|
|
for (const list of found) for (const issue of list) issues.push(issue);
|
|
|
|
|
};
|