Skip to content

Commit f55990e

Browse files
fix(cli): name the network cause of a failed fetch in the error box (#7236)
1 parent 5ee9e92 commit f55990e

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

‎packages/cli/src/cloud/errors.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,17 @@ describe("cloud/errors reportApiError", () => {
167167
expect(() => reportApiError("Weird failure", "just a string")).toThrow(CliRuntimeError);
168168
expect(errorBox).toHaveBeenCalledWith("Weird failure", "just a string", undefined);
169169
});
170+
171+
it("names the network cause of a failed fetch on the third line, and nothing for a free-text cause", () => {
172+
const cause = Object.assign(new Error("connect ETIMEDOUT 1.2.3.4:443"), { code: "ETIMEDOUT" });
173+
expect(() => reportApiError("Upload failed", new TypeError("fetch failed", { cause }))).toThrow(
174+
CliRuntimeError,
175+
);
176+
expect(errorBox).toHaveBeenLastCalledWith("Upload failed", "fetch failed", "code: ETIMEDOUT");
177+
const odd = Object.assign(new Error("x"), { code: "see /Users/me/file.mp4" });
178+
expect(() =>
179+
reportApiError("Upload failed", new TypeError("fetch failed", { cause: odd })),
180+
).toThrow(CliRuntimeError);
181+
expect(errorBox).toHaveBeenLastCalledWith("Upload failed", "fetch failed", undefined);
182+
});
170183
});

‎packages/cli/src/cloud/errors.ts‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ function hintCode(err: HyperframesApiError): string | undefined {
5656
* errors) so the user can recover without having to remember the
5757
* render_id.
5858
*/
59+
/** `code: ETIMEDOUT` for a failed fetch, whose own message is only "fetch failed"; the cause names why. */
60+
function networkCode(err: Error): string | undefined {
61+
const code = (err.cause as { code?: unknown } | undefined)?.code;
62+
return typeof code === "string" && /^[A-Z][A-Z0-9_]{2,40}$/.test(code)
63+
? `code: ${code}`
64+
: undefined;
65+
}
66+
5967
// fallow-ignore-next-line complexity
6068
export function reportApiError(
6169
stage: string,
@@ -95,7 +103,7 @@ export function reportApiError(
95103
failCommand();
96104
}
97105
if (err instanceof Error) {
98-
errorBox(stage, err.message, options.suggestion);
106+
errorBox(stage, err.message, options.suggestion ?? networkCode(err));
99107
failCommand();
100108
}
101109
errorBox(stage, String(err), options.suggestion);

0 commit comments

Comments
 (0)