fix(cli): name the network cause of a failed fetch in the error box (#7236)

This commit is contained in:
Miguel Ángel
2026-11-05 13:08:09 +00:00
committed by GitHub
parent 5ee9e928d5
commit f55990ebca
2 changed files with 22 additions and 1 deletions
+13
View File
@@ -167,4 +167,17 @@ describe("cloud/errors reportApiError", () => {
expect(() => reportApiError("Weird failure", "just a string")).toThrow(CliRuntimeError);
expect(errorBox).toHaveBeenCalledWith("Weird failure", "just a string", undefined);
});
it("names the network cause of a failed fetch on the third line, and nothing for a free-text cause", () => {
const cause = Object.assign(new Error("connect ETIMEDOUT 1.2.3.4:443"), { code: "ETIMEDOUT" });
expect(() => reportApiError("Upload failed", new TypeError("fetch failed", { cause }))).toThrow(
CliRuntimeError,
);
expect(errorBox).toHaveBeenLastCalledWith("Upload failed", "fetch failed", "code: ETIMEDOUT");
const odd = Object.assign(new Error("x"), { code: "see /Users/me/file.mp4" });
expect(() =>
reportApiError("Upload failed", new TypeError("fetch failed", { cause: odd })),
).toThrow(CliRuntimeError);
expect(errorBox).toHaveBeenLastCalledWith("Upload failed", "fetch failed", undefined);
});
});
+9 -1
View File
@@ -56,6 +56,14 @@ function hintCode(err: HyperframesApiError): string | undefined {
* errors) so the user can recover without having to remember the
* render_id.
*/
/** `code: ETIMEDOUT` for a failed fetch, whose own message is only "fetch failed"; the cause names why. */
function networkCode(err: Error): string | undefined {
const code = (err.cause as { code?: unknown } | undefined)?.code;
return typeof code === "string" && /^[A-Z][A-Z0-9_]{2,40}$/.test(code)
? `code: ${code}`
: undefined;
}
// fallow-ignore-next-line complexity
export function reportApiError(
stage: string,
@@ -95,7 +103,7 @@ export function reportApiError(
failCommand();
}
if (err instanceof Error) {
errorBox(stage, err.message, options.suggestion);
errorBox(stage, err.message, options.suggestion ?? networkCode(err));
failCommand();
}
errorBox(stage, String(err), options.suggestion);