Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add link to dashboard when Queue commands fail due to being unauthorized #2208

Merged
merged 5 commits into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fast-dryers-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

Add link to Queues tab in dashboard when unauthorized to use Queues
65 changes: 64 additions & 1 deletion packages/wrangler/src/__tests__/queues.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { type QueueResponse, type PostConsumerBody } from "../queues/client";
import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
import { setMockResponse, unsetAllMocks } from "./helpers/mock-cfetch";
import {
createFetchResult,
setMockRawResponse,
setMockResponse,
unsetAllMocks,
} from "./helpers/mock-cfetch";
import { mockConsoleMethods } from "./helpers/mock-console";
import { runInTempDir } from "./helpers/run-in-tmp";
import { runWrangler } from "./helpers/run-wrangler";
Expand Down Expand Up @@ -157,6 +162,35 @@ describe("wrangler", () => {
`);
expect(requests.count).toEqual(1);
});

it("should show link to dash when not enabled", async () => {
const queueName = "testQueue";
setMockRawResponse(
"/accounts/:accountId/workers/queues",
([_url, accountId]) => {
expect(accountId).toEqual("some-account-id");
return createFetchResult(null, false, [
{ message: "workers.api.error.unauthorized", code: 10023 },
]);
}
);
await expect(
runWrangler(`queues create ${queueName}`)
).rejects.toThrowError();
expect(std.out).toMatchInlineSnapshot(`
"Creating queue testQueue.
Queues is not currently enabled on this account. Go to https://dash.cloudflare.com/some-account-id/workers/queues to enable it.

X [ERROR] A request to the Cloudflare API (/accounts/some-account-id/workers/queues) failed.

workers.api.error.unauthorized [code: 10023]

If you think this is a bug, please open an issue at:
https://github.com/cloudflare/wrangler2/issues/new/choose

"
`);
});
});

describe("delete", () => {
Expand Down Expand Up @@ -309,6 +343,35 @@ describe("wrangler", () => {
Added consumer to queue testQueue."
`);
});

it("should show link to dash when not enabled", async () => {
const queueName = "testQueue";
setMockRawResponse(
`/accounts/:accountId/workers/queues/${queueName}/consumers`,
([_url, accountId]) => {
expect(accountId).toEqual("some-account-id");
return createFetchResult(null, false, [
{ message: "workers.api.error.unauthorized", code: 10023 },
]);
}
);
await expect(
runWrangler(`queues consumer add ${queueName} testScript`)
).rejects.toThrowError();
expect(std.out).toMatchInlineSnapshot(`
"Adding consumer to queue testQueue.
Queues is not currently enabled on this account. Go to https://dash.cloudflare.com/some-account-id/workers/queues to enable it.

X [ERROR] A request to the Cloudflare API (/accounts/some-account-id/workers/queues/testQueue/consumers) failed.

workers.api.error.unauthorized [code: 10023]

If you think this is a bug, please open an issue at:
https://github.com/cloudflare/wrangler2/issues/new/choose

"
`);
});
});

describe("delete", () => {
Expand Down
3 changes: 3 additions & 0 deletions packages/wrangler/src/queues/cli/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { type BuilderCallback } from "yargs";
import { type CommonYargsOptions } from "../../../yargs-types";
import { HandleUnauthorizedError } from "../../utils";
import { consumers } from "./consumer";

import { options as createOptions, handler as createHandler } from "./create";
Expand Down Expand Up @@ -30,4 +31,6 @@ export const queues: BuilderCallback<CommonYargsOptions, unknown> = (yargs) => {
await consumers(consumersYargs);
}
);

yargs.fail(HandleUnauthorizedError);
};
18 changes: 18 additions & 0 deletions packages/wrangler/src/queues/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { logger } from "../logger";
import { type ParseError } from "../parse";
import { getAccountId } from "../user";

const isFetchError = (err: unknown): err is ParseError => err instanceof Error;

export const HandleUnauthorizedError = async (_msg: string, err: Error) => {
//@ts-expect-error non-standard property on Error
if (isFetchError(err) && err.code === 10023) {
const accountId = await getAccountId();
if (accountId) {
return logger.log(
`Queues is not currently enabled on this account. Go to https://dash.cloudflare.com/${accountId}/workers/queues to enable it.`
);
}
}
throw err;
};