mirror of
https://github.com/heygen-com/hyperframes.git
synced 2026-11-25 08:32:45 +00:00
fix(cli): a crashed or encoder-failed render tells Desktop its code (#8434)
* test(cli): a render's encoder failure or crash names its code for Desktop * fix(cli): a crashed or encoder-failed render tells Desktop its code * fix(cli): an encoder failure is named by its encoder class before a file error * test(cli): a bad source clip or a full disk is not named an encoder exit * fix(cli): only the encoder's own failures are named encoder exits, file codes first * fix(cli): a usage error or a --json render keeps stdout free of the code line
This commit is contained in:
@@ -96,6 +96,7 @@ import {
|
||||
import { resolveCommandUsage, type ResolvedCommandUsage } from "./utils/commandUsageResolution.js";
|
||||
import { closestMatch, closestNames, commandRedirect, fileHint } from "./utils/didYouMean.js";
|
||||
import { errorBox } from "./ui/format.js";
|
||||
import { renderExitDiagnostic } from "./ui/renderDiagnostic.js";
|
||||
import { failureShown, jsonErrorBody, watchOutput, writeJsonFailure } from "./utils/jsonError.js";
|
||||
import { isDevMode } from "./utils/env.js";
|
||||
import { terminalLines, terminalText } from "@hyperframes/core";
|
||||
@@ -527,6 +528,11 @@ function sayCrash(error: Error): void {
|
||||
errorBox("The command stopped on an unexpected error", words.filter(Boolean).join("\n"));
|
||||
}
|
||||
|
||||
const quietRun = argv.includes("--quiet");
|
||||
function sayRenderCode(error: unknown, fallback: string): void {
|
||||
if (command === "render" && !quietRun && !hasJsonFlag) renderExitDiagnostic(error, fallback);
|
||||
}
|
||||
|
||||
/** The first caller line of a stack, past the Error's own line and the result bookkeeping that recorded it. */
|
||||
const firstFrameOf = (stack: string | undefined) =>
|
||||
stack
|
||||
@@ -555,6 +561,7 @@ function exitAfterCliFailure(
|
||||
commandFailed = true;
|
||||
sayCrash(error);
|
||||
emitCliErrorEvent(kind, error);
|
||||
sayRenderCode(error, kind.toUpperCase());
|
||||
_flushSync?.();
|
||||
process.exit(1);
|
||||
}
|
||||
@@ -590,6 +597,7 @@ process.on("unhandledRejection", (reason) => {
|
||||
process.exitCode = 1;
|
||||
sayCrash(error);
|
||||
emitCliErrorEvent("unhandled_rejection", error);
|
||||
sayRenderCode(error, "UNHANDLED_REJECTION");
|
||||
});
|
||||
|
||||
// Lazy-load help renderer — avoids allocating help data on non-help invocations
|
||||
@@ -706,6 +714,7 @@ async function executeCli(): Promise<void> {
|
||||
commandFailed = true;
|
||||
errorReported = true;
|
||||
await reportCommandFailure(command, error);
|
||||
if (result.kind !== "usage_error") sayRenderCode(error, "RENDER_FAILED");
|
||||
const typed = error instanceof CliUsageError || error instanceof CliRuntimeError;
|
||||
const usage = error instanceof CliUsageError && !error.result.presented;
|
||||
if (usage && !error.hint) await showRequestedUsage(console.error);
|
||||
|
||||
@@ -1164,7 +1164,7 @@ async function executeLocalRender(
|
||||
? "."
|
||||
: ", or render WebM instead: hyperframes render --format webm --output output.webm"),
|
||||
);
|
||||
failCommand();
|
||||
failCommand(1, error);
|
||||
}
|
||||
// Capability probing is advisory. Let the real encode surface the
|
||||
// authoritative FFmpeg error instead of failing here with a bare stack.
|
||||
|
||||
@@ -149,6 +149,30 @@ describe("render diagnostic wire", () => {
|
||||
"CAPTURE_PROTOCOL_TIMEOUT",
|
||||
],
|
||||
[new Error(PRIVATE), "RENDER_FAILED"],
|
||||
[
|
||||
Object.assign(new Error(PRIVATE), { name: "SegmentEncoderSpawnError" }),
|
||||
"SEGMENT_ENCODER_SPAWN",
|
||||
],
|
||||
[
|
||||
Object.assign(new Error(`spawn ffmpeg ENOENT ${PRIVATE}`), {
|
||||
name: "SegmentEncoderSpawnError",
|
||||
}),
|
||||
"SEGMENT_ENCODER_SPAWN",
|
||||
],
|
||||
[new Error(`Faststart failed: FFmpeg exited with code 1 ${PRIVATE}`), "FASTSTART_FAILED"],
|
||||
[new Error(`Audio muxing failed: ${PRIVATE}`), "AUDIO_MUX_FAILED"],
|
||||
[new Error(`Encoding failed: FFmpeg exited with code 1 ${PRIVATE}`), "ENCODER_EXITED"],
|
||||
[new Error(`Segment 2 encode failed: ${PRIVATE}`), "ENCODER_EXITED"],
|
||||
[
|
||||
new Error(`Streaming encoder exited before frame 0 was written: ${PRIVATE}`),
|
||||
"ENCODER_EXITED",
|
||||
],
|
||||
[new Error(`Faststart failed: ENOSPC: no space left on device ${PRIVATE}`), "ENOSPC"],
|
||||
[new Error(`FFmpeg exited with code 1: ${PRIVATE}`), "RENDER_FAILED"],
|
||||
[
|
||||
new Error(`Animated GIF transcode failed: FFmpeg exited with code 1 ${PRIVATE}`),
|
||||
"RENDER_FAILED",
|
||||
],
|
||||
])("names a failed render's code without its words (%#)", (error, code) => {
|
||||
const line = capture(() => renderFailureDiagnostic(undefined, error, false));
|
||||
expect(JSON.parse(line.slice("@hf-diagnostic ".length))).toEqual({
|
||||
@@ -159,6 +183,19 @@ describe("render diagnostic wire", () => {
|
||||
expect(line).not.toContain(PRIVATE);
|
||||
});
|
||||
|
||||
it("names a render that stopped outside its own report, once", async () => {
|
||||
vi.resetModules();
|
||||
const { renderExitDiagnostic } = await import("./renderDiagnostic.js");
|
||||
const crash = Object.assign(new Error(PRIVATE), {
|
||||
cause: Object.assign(new Error(PRIVATE), { name: "H264EncoderUnavailableError" }),
|
||||
});
|
||||
const line = capture(() => renderExitDiagnostic(crash, "UNCAUGHT_EXCEPTION"));
|
||||
expect(line).toBe(
|
||||
'@hf-diagnostic {"version":1,"operation":"render","code":"H264_ENCODER_UNAVAILABLE"}\n',
|
||||
);
|
||||
expect(capture(() => renderExitDiagnostic(new Error(PRIVATE), "UNCAUGHT_EXCEPTION"))).toBe("");
|
||||
});
|
||||
|
||||
it("stays silent for a terminal or a container attached to a terminal", () => {
|
||||
expect(capture([failure], true)).toBe("");
|
||||
vi.unstubAllEnvs();
|
||||
|
||||
@@ -48,19 +48,53 @@ function captureCodeOf(error: unknown, message: string): string | undefined {
|
||||
return error.pageErrorLines.length > 0 ? "PAGE_ERROR" : `CAPTURE_${error.kind.toUpperCase()}`;
|
||||
}
|
||||
|
||||
const ENCODER_ERRORS: Record<string, string> = {
|
||||
SegmentEncoderSpawnError: "SEGMENT_ENCODER_SPAWN",
|
||||
H264EncoderUnavailableError: "H264_ENCODER_UNAVAILABLE",
|
||||
};
|
||||
const ENCODER_MESSAGES: Array<[RegExp, string]> = [
|
||||
[/^Faststart failed\b/, "FASTSTART_FAILED"],
|
||||
[/^Audio muxing failed\b/, "AUDIO_MUX_FAILED"],
|
||||
[
|
||||
/^(?:Encoding|Streaming encode|HDR encode|Segment \d+ encode) failed\b|^Streaming encoder exited before frame\b/,
|
||||
"ENCODER_EXITED",
|
||||
],
|
||||
];
|
||||
|
||||
function causesOf(error: unknown): Error[] {
|
||||
const causes: Error[] = [];
|
||||
for (let seen = error; causes.length < 5 && seen instanceof Error; seen = seen.cause)
|
||||
causes.push(seen);
|
||||
return causes;
|
||||
}
|
||||
|
||||
const encoderClassOf = (error: unknown) =>
|
||||
causesOf(error)
|
||||
.map((seen) => ENCODER_ERRORS[seen.name])
|
||||
.find(Boolean);
|
||||
|
||||
const encoderMessageOf = (error: unknown) =>
|
||||
causesOf(error)
|
||||
.map((seen) => ENCODER_MESSAGES.find(([words]) => words.test(seen.message))?.[1])
|
||||
.find(Boolean);
|
||||
|
||||
/** A failed render's code: never its words, which can name the person's files and folders. */
|
||||
function renderFailureCode(error: unknown): string {
|
||||
function renderFailureCode(error: unknown, fallback = "RENDER_FAILED"): string {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
if (error instanceof RenderQualityError && error.warnings[0])
|
||||
return error.warnings[0].code.toUpperCase();
|
||||
return (
|
||||
producerCodeOf(error) ??
|
||||
encoderClassOf(error) ??
|
||||
fileErrorOf(error, message) ??
|
||||
encoderMessageOf(error) ??
|
||||
captureCodeOf(error, message) ??
|
||||
"RENDER_FAILED"
|
||||
fallback
|
||||
);
|
||||
}
|
||||
|
||||
let codeWritten = false;
|
||||
|
||||
/** Versioned, content-free failure line for Desktop's piped CLI, separate from human error output. */
|
||||
export function renderMachineDiagnostic(values: readonly unknown[], code?: string): void {
|
||||
if (process.stdout.isTTY || process.env.HYPERFRAMES_STDOUT_IS_TTY === "1") return;
|
||||
@@ -73,6 +107,11 @@ export function renderMachineDiagnostic(values: readonly unknown[], code?: strin
|
||||
...(failures.length > 0 ? { audioFailures: failures } : {}),
|
||||
};
|
||||
process.stdout.write(`@hf-diagnostic ${JSON.stringify(line)}\n`);
|
||||
if (code) codeWritten = true;
|
||||
}
|
||||
|
||||
export function renderExitDiagnostic(error: unknown, fallback: string): void {
|
||||
if (!codeWritten) renderMachineDiagnostic([], renderFailureCode(error, fallback));
|
||||
}
|
||||
|
||||
export function renderFailureDiagnostic(
|
||||
|
||||
Reference in New Issue
Block a user