Skip to content

Commit

Permalink
fix: suggest trying to update Wrangler if there is a newer one availa…
Browse files Browse the repository at this point in the history
…ble after an unexpected error (#5746)
  • Loading branch information
petebacondarwin committed May 2, 2024
1 parent 769187e commit 1dd9f7e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/stale-parrots-explode.md
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

fix: suggest trying to update Wrangler if there is a newer one available after an unexpected error
20 changes: 20 additions & 0 deletions packages/wrangler/src/__tests__/index.test.ts
@@ -1,4 +1,6 @@
import { logPossibleBugMessage } from "..";
import { getPackageManager } from "../package-manager";
import { updateCheck } from "../update-check";
import { endEventLoop } from "./helpers/end-event-loop";
import { mockConsoleMethods } from "./helpers/mock-console";
import { runInTempDir } from "./helpers/run-in-tmp";
Expand Down Expand Up @@ -279,4 +281,22 @@ describe("wrangler", () => {
--dry-run: exiting now."
`);
});

describe("logPossibleBugMessage()", () => {
it("should display a 'possible bug' message", async () => {
await logPossibleBugMessage();
expect(std.out).toMatchInlineSnapshot(
`"If you think this is a bug then please create an issue at https://github.com/cloudflare/workers-sdk/issues/new/choose"`
);
});

it("should display a 'try updating' message if there is one available", async () => {
(updateCheck as jest.Mock).mockImplementation(async () => "123.123.123");
await logPossibleBugMessage();
expect(std.out).toMatchInlineSnapshot(`
"If you think this is a bug then please create an issue at https://github.com/cloudflare/workers-sdk/issues/new/choose
Note that there is a newer version of Wrangler available (123.123.123). Consider checking whether upgrading resolves this error."
`);
});
});
});
23 changes: 18 additions & 5 deletions packages/wrangler/src/index.ts
Expand Up @@ -58,7 +58,7 @@ import {
import { tailHandler, tailOptions } from "./tail";
import registerTriggersSubcommands from "./triggers";
import { typesHandler, typesOptions } from "./type-generation";
import { printWranglerBanner } from "./update-check";
import { printWranglerBanner, updateCheck } from "./update-check";
import {
getAuthFromEnv,
listScopes,
Expand Down Expand Up @@ -839,10 +839,7 @@ export async function main(argv: string[]): Promise<void> {
} else {
logger.error(e instanceof Error ? e.message : e);
if (!(e instanceof UserError)) {
logger.log(
`${fgGreenColor}%s${resetColor}`,
"If you think this is a bug then please create an issue at https://github.com/cloudflare/workers-sdk/issues/new/choose"
);
await logPossibleBugMessage();
}
}

Expand Down Expand Up @@ -910,4 +907,20 @@ export function getDevCompatibilityDate(
return compatibilityDate ?? currentDate;
}

/**
* Write a message to the log that tells the user what they might do after we have reported an unexpected error.
*/
export async function logPossibleBugMessage() {
logger.log(
`${fgGreenColor}%s${resetColor}`,
"If you think this is a bug then please create an issue at https://github.com/cloudflare/workers-sdk/issues/new/choose"
);
const latestVersion = await updateCheck();
if (latestVersion) {
logger.log(
`Note that there is a newer version of Wrangler available (${latestVersion}). Consider checking whether upgrading resolves this error.`
);
}
}

export { printWranglerBanner };

0 comments on commit 1dd9f7e

Please sign in to comment.