diff --git a/packages/cli/src/cloud/errors.test.ts b/packages/cli/src/cloud/errors.test.ts index d746104f18..63cf0685ce 100644 --- a/packages/cli/src/cloud/errors.test.ts +++ b/packages/cli/src/cloud/errors.test.ts @@ -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); + }); }); diff --git a/packages/cli/src/cloud/errors.ts b/packages/cli/src/cloud/errors.ts index 4ed2f862f3..f915b9651a 100644 --- a/packages/cli/src/cloud/errors.ts +++ b/packages/cli/src/cloud/errors.ts @@ -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);