fix(cli): error boxes and command failure lines print control characters as visible escapes (#7987)

* test(cli): error boxes and diff's failure line print control characters as escapes

* fix(cli): error boxes and command failure lines print control characters as visible escapes

* style(cli): format the escaped error lines
This commit is contained in:
Miguel Ángel
2026-11-17 00:31:19 +00:00
committed by GitHub
parent 028ab75535
commit 5979991d5f
14 changed files with 97 additions and 18 deletions
+1 -1
View File
@@ -476,7 +476,7 @@ function reportPostRenderTerminationEvent(
error: Error,
): void {
process.stderr.write(
` [hyperframes] Late ${label} (the command already delivered its result): ${error.message}\n`,
` [hyperframes] Late ${label} (the command already delivered its result): ${terminalText(error.message)}\n`,
);
emitCliErrorEvent(kind, error);
}
+2 -2
View File
@@ -135,7 +135,7 @@ export default defineCommand({
try {
captureUpstreamProxy();
} catch (error) {
console.error(normalizeErrorMessage(error));
console.error(terminalLines(normalizeErrorMessage(error)));
failCommand();
}
@@ -162,7 +162,7 @@ export default defineCommand({
try {
url = resolveLocalCaptureTarget(localFile, localRoot).fileUrl;
} catch (err) {
console.error(normalizeErrorMessage(err));
console.error(terminalLines(normalizeErrorMessage(err)));
failCommand();
}
} else {
+1 -1
View File
@@ -263,7 +263,7 @@ export async function runVideoMode(args: VideoModeArgs): Promise<void> {
try {
ensureCaptureDirSync(projectDir, outDir);
} catch (e) {
console.error(`${c.error("✗")} ${(e as Error).message}`);
console.error(`${c.error("✗")} ${terminalText((e as Error).message)}`);
setCommandExitCode(1);
return;
}
+1 -1
View File
@@ -416,7 +416,7 @@ function reportCheckFailure(
const code = usage ? error.code : "runtime_error";
console.log(JSON.stringify(addMeta(jsonFailure(code, message)), null, 2));
} else {
console.error(`${c.error("✗")} Check failed: ${message}`);
console.error(`${c.error("✗")} Check failed: ${terminalText(message)}`);
}
if (usage) failUsage();
setCommandExitCode(1);
+40
View File
@@ -0,0 +1,40 @@
import { runCommand } from "citty";
import { afterEach, describe, expect, it, vi } from "vitest";
import { CliRuntimeError } from "../utils/commandResult.js";
vi.mock("../measure/diff.js", async (importOriginal) => ({
...(await importOriginal<typeof import("../measure/diff.js")>()),
diffVideos: vi.fn(),
}));
afterEach(() => {
vi.restoreAllMocks();
});
describe("diff command terminal output", () => {
it("prints a page error with control characters as visible escapes", async () => {
const { diffVideos } = await import("../measure/diff.js");
vi.mocked(diffVideos).mockRejectedValueOnce(
new Error("page threw \u001b[2J\u009b\nsecond line \u001b]0;t\u0007"),
);
const printed: string[] = [];
const keep = (...args: unknown[]) => {
printed.push(args.map(String).join(" "));
};
vi.spyOn(console, "log").mockImplementation(keep);
vi.spyOn(console, "error").mockImplementation(keep);
vi.spyOn(process.stderr, "write").mockImplementation(() => true);
vi.spyOn(process, "exit").mockImplementation(() => undefined as never);
const { default: diff } = await import("./diff.js");
await runCommand(diff, { rawArgs: ["original.mp4", "yours.mp4"] }).catch((error: unknown) => {
if (!(error instanceof CliRuntimeError)) throw error;
});
const text = printed.join("\n");
expect(text).toContain("page threw \\x1b[2J\\x9b\nsecond line \\x1b]0;t\\x07");
for (const control of ["\u001b[2J", "\u009b", "\u001b]", "\u0007"]) {
expect(text).not.toContain(control);
}
});
});
+5 -1
View File
@@ -1,3 +1,4 @@
import { terminalLines } from "@hyperframes/core";
import { basename, extname, join, resolve } from "node:path";
import { defineCommand } from "../utils/defineCommand.js";
import type { Example } from "./_examples.js";
@@ -294,7 +295,10 @@ export default defineCommand({
const message = normalizeErrorMessage(err);
if (json)
console.log(formatJson(withMeta({ ok: false, error: message, ...(fix ? { fix } : {}) })));
else console.error(`${c.error("✗")} ${message}${fix ? `\n Fix: ${fix}` : ""}`);
else
console.error(
`${c.error("✗")} ${terminalLines(message)}${fix ? `\n Fix: ${terminalLines(fix)}` : ""}`,
);
failCommand(1, err);
} finally {
scheduleCliExit(exitCode);
+1 -1
View File
@@ -418,7 +418,7 @@ function resolveMotionSpec(specPath: string, json: boolean): MotionSpec {
),
);
} else {
console.error(`${c.error("✗")} ${message}`);
console.error(`${c.error("✗")} ${terminalLines(message)}`);
}
failCommand();
}
+2 -1
View File
@@ -8,6 +8,7 @@ import {
isPathInside,
normalizeHfColorGrading,
serializeHfColorGrading,
terminalLines,
} from "@hyperframes/core";
import {
COLOR_GRADING_SOURCE_TAGS,
@@ -805,7 +806,7 @@ function printFailure(error: unknown, json: boolean): void {
console.log(
JSON.stringify(withMeta(jsonFailure(usage ? error.code : "runtime_error", message))),
);
} else console.error(`${c.error("✗")} ${message}`);
} else console.error(`${c.error("✗")} ${terminalLines(message)}`);
if (usage) failUsage();
failCommand(1, error);
}
+3 -2
View File
@@ -1,3 +1,4 @@
import { terminalText } from "@hyperframes/core";
import { setCommandExitCode, requestCliExit } from "../utils/commandResult.js";
// fallow-ignore-file code-duplication
import { defineCommand } from "../utils/defineCommand.js";
@@ -1223,7 +1224,7 @@ async function printCurrentContext(
if (selection.ok) {
console.log(` ${c.dim("Selection")} ${selection.value.label}`);
} else {
console.log(` ${c.dim("Selection")} ${selection.error.message}`);
console.log(` ${c.dim("Selection")} ${terminalText(selection.error.message)}`);
}
}
if (contextIncludes(fields, "lint")) {
@@ -1232,7 +1233,7 @@ async function printCurrentContext(
` ${c.dim("Lint")} ${lint.summary.errors} error(s), ${lint.summary.warnings} warning(s)`,
);
} else {
console.log(` ${c.dim("Lint")} ${lint.error.message}`);
console.log(` ${c.dim("Lint")} ${terminalText(lint.error.message)}`);
}
}
console.log();
+3 -1
View File
@@ -1667,7 +1667,9 @@ export async function selfClosedMediaWarning(source: string): Promise<string | n
}
function warnSnapshotsNotRecorded(error: unknown): undefined {
console.warn(` ${c.warn("⚠")} Snapshots were not recorded: ${normalizeErrorMessage(error)}`);
console.warn(
` ${c.warn("⚠")} Snapshots were not recorded: ${terminalText(normalizeErrorMessage(error))}`,
);
return undefined;
}
+3 -1
View File
@@ -1,3 +1,4 @@
import { terminalLines } from "@hyperframes/core";
// What every `hyperframes measure <op>` shares: one JSON object on stdout (summaries, never per-frame arrays;
// pictures as file paths), progress on stderr, and an error that says what to run instead.
import { c } from "../ui/colors.js";
@@ -30,7 +31,8 @@ export async function runMeasure(work: () => object | Promise<object>): Promise<
}),
),
);
if (process.stdout.isTTY) console.error(`${c.error("✗")} ${normalizeErrorMessage(err)}`);
if (process.stdout.isTTY)
console.error(`${c.error("✗")} ${terminalLines(normalizeErrorMessage(err))}`);
failCommand(1, err);
}
}
+28 -1
View File
@@ -1,6 +1,7 @@
import { describe, expect, it } from "vitest";
import { describe, expect, it, vi } from "vitest";
import {
errorBox,
formatBytes,
formatDuration,
formatRenderPipelineDetail,
@@ -197,3 +198,29 @@ describe("formatBytes", () => {
expect(formatBytes(1_048_576)).toBe("1.0 MB");
});
});
describe("errorBox", () => {
it("prints control characters in its title, hint and suggestion as visible escapes", () => {
const said: string[] = [];
const spy = vi.spyOn(console, "error").mockImplementation((...args: unknown[]) => {
said.push(args.map(String).join(" "));
});
try {
errorBox(
"Load \u001b]0;t\u0007",
"page threw \u001b[2J\u009b\nsecond line",
"run \u001b[9;9H",
);
} finally {
spy.mockRestore();
}
const text = said.join("\n");
expect(text).toContain("Load \\x1b]0;t\\x07");
expect(text).toContain("page threw \\x1b[2J\\x9b");
expect(text).toContain(" second line");
expect(text).toContain("run \\x1b[9;9H");
for (const control of ["\u001b]", "\u0007", "\u009b", "\u001b[2J", "\u001b[9;9H"]) {
expect(text).not.toContain(control);
}
});
});
+4 -3
View File
@@ -1,4 +1,5 @@
import type { BrowserGpuMode } from "../browser/gpuPolicy.js";
import { terminalLines, terminalText } from "@hyperframes/core";
import { c } from "./colors.js";
import { recordFailure } from "../utils/jsonError.js";
@@ -132,17 +133,17 @@ export function label(name: string, value: string): string {
export function errorBox(title: string, hint?: string, suggestion?: string): void {
recordFailure(title, hint ?? suggestion);
console.error(`\n${c.error("\u2717")} ${c.bold(title)}`);
console.error(`\n${c.error("\u2717")} ${c.bold(terminalText(title))}`);
if (hint) {
// Indent EVERY hint line, not just the first \u2014 a multi-line hint (e.g. the
// NO_TOKEN numbered setup list) otherwise had line 1 indented and the rest
// flush-left, mangling the list. Single-line hints are unchanged.
const indented = hint
const indented = terminalLines(hint)
.split("\n")
.map((line) => ` ${line}`)
.join("\n");
console.error(`\n${c.dim(indented)}`);
}
if (suggestion) console.error(` ${c.accent(suggestion)}`);
if (suggestion) console.error(` ${c.accent(terminalLines(suggestion))}`);
console.error();
}
+3 -2
View File
@@ -1,3 +1,4 @@
import { terminalLines } from "@hyperframes/core";
import { DEFAULT_CONFIG } from "@hyperframes/engine";
import { BrowserCleanupError, onStopSignal } from "../browser/launch.js";
import type { CheckProgress } from "./checkProgress.js";
@@ -1904,7 +1905,7 @@ async function withFindingCrops(
}
// Still non-gating, but observable: rollouts need the crop-failure rate
// (a second Chrome launch failing/timing out) without failing the run.
console.error(" finding crops skipped: " + normalizeErrorMessage(error));
console.error(" finding crops skipped: " + terminalLines(normalizeErrorMessage(error)));
trackCommandFailure("check-finding-crops", error);
return report;
}
@@ -1936,7 +1937,7 @@ async function withOptionalFindingFrames(
const finding = runtimeFailure(error);
return { ...report, ok: false, runtime: section([...report.runtime.findings, finding]) };
}
console.error(" finding frames skipped: " + normalizeErrorMessage(error));
console.error(" finding frames skipped: " + terminalLines(normalizeErrorMessage(error)));
trackCommandFailure("check-finding-frames", error);
return report;
}