Skip to content

Commit

Permalink
Changed yargs.fail error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
pmiguel committed Feb 28, 2024
1 parent 77a75c9 commit 68b9d1b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 15 deletions.
33 changes: 27 additions & 6 deletions packages/wrangler/src/__tests__/queues.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { rest } from "msw";
import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
import { mockConsoleMethods } from "./helpers/mock-console";
import { clearDialogs } from "./helpers/mock-dialogs";
import { useMockIsTTY } from "./helpers/mock-istty";
import { msw } from "./helpers/msw";
import { runInTempDir } from "./helpers/run-in-tmp";
import { runWrangler } from "./helpers/run-wrangler";
Expand All @@ -12,6 +14,14 @@ describe("wrangler", () => {
runInTempDir();
const std = mockConsoleMethods();

const { setIsTTY } = useMockIsTTY();
beforeEach(() => {
setIsTTY(true);
});
afterEach(() => {
clearDialogs();
});

describe("queues", () => {
it("should show the correct help text", async () => {
await runWrangler("queues --help");
Expand Down Expand Up @@ -206,8 +216,8 @@ describe("wrangler", () => {
-v, --version Show version number [boolean]
Options:
--delivery-delay How long a published messages should be delayed for, in seconds. Must be a positive integer [number]
--no-delivery-delay Sets published messages to have no delay [boolean]"
--no-delivery-delay Sets published messages to have no delay [boolean]
--delivery-delay How long a published messages should be delayed for, in seconds. Must be a positive integer [number]"
`);
});

Expand Down Expand Up @@ -283,9 +293,12 @@ describe("wrangler", () => {
it("should show an error with delivery delay and no delivery delay are used", async () => {
const requests = mockCreateRequest("testQueue", { delivery_delay: 0 });

expect(
runWrangler("queues create testQueue --no-delivery-delay --delivery-delay=10"))
.toThrowErrorMatchingInlineSnapshot(`"Error: can't use --no-delivery-delay with --delivery-delay"`
await expect(
runWrangler(
"queues create testQueue --no-delivery-delay --delivery-delay=10"
)
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Error: can't use more than a delay setting."`
);

expect(requests.count).toEqual(0);
Expand Down Expand Up @@ -347,6 +360,14 @@ describe("wrangler", () => {
});
});

describe("update", () => {
it("A", async () => {
await expect(
runWrangler("queues update myQueue")
).rejects.toThrowErrorMatchingInlineSnapshot(`"boom"`);
});
});

describe("consumers", () => {
it("should show the correct help text", async () => {
await runWrangler("queues consumer --help");
Expand Down Expand Up @@ -517,7 +538,7 @@ describe("wrangler", () => {
"queues consumer add testQueue testScript --env myEnv --batch-size 20 --batch-timeout 10 --message-retries 3 --max-concurrency 3 --dead-letter-queue myDLQ --no-retry-delay --retry-delay=10"
)
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Error: can't use --no-retry-delay with --retry-delay"`
`"Error: can't use more than a delay setting."`
);

expect(requests.count).toEqual(0);
Expand Down
4 changes: 2 additions & 2 deletions packages/wrangler/src/queues/cli/commands/consumer/add.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { readConfig } from "../../../../config";
import { logger } from "../../../../logger";
import { UserError } from "../../../../errors";
import type { PostConsumerBody } from "../../../client";
import { postConsumer } from "../../../client";
import type { CommonYargsArgv, StrictYargsOptionsToInterface } from "../../../../yargs-types";
import { handleFetchError } from "../../../utils";
import { CommandLineArgsError } from "../../../../index";

export function options(yargs: CommonYargsArgv) {
return yargs
Expand Down Expand Up @@ -75,7 +75,7 @@ function createBody(args: StrictYargsOptionsToInterface<typeof options>): PostCo
// Workaround, Yargs does not play nicely with both --parameter and --no-parameter set.
// Negating a number parameter returns 0, making retryDelay an array with [0, <value>]
if(Array.isArray(args.retryDelay)) {
throw new UserError(`Error: can't use --no-retry-delay with --retry-delay`);
throw new CommandLineArgsError(`Error: can't use more than a delay setting.`);
}

if(args.retryDelay != undefined) {
Expand Down
12 changes: 6 additions & 6 deletions packages/wrangler/src/queues/cli/commands/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import type {
CommonYargsArgv,
StrictYargsOptionsToInterface,
} from "../../../yargs-types";
import { UserError } from "../../../errors";
import { handleFetchError } from "../../utils";
import { CommandLineArgsError } from "../../../index";

export function options(yargs: CommonYargsArgv) {
return yargs
Expand All @@ -17,14 +17,14 @@ export function options(yargs: CommonYargsArgv) {
description: "The name of the queue",
})
.options({
"delivery-delay": {
type: "number",
describe: "How long a published messages should be delayed for, in seconds. Must be a positive integer",
},
"no-delivery-delay": {
type: "boolean",
describe: "Sets published messages to have no delay",
boolean: true,
},
"delivery-delay": {
type: "number",
describe: "How long a published messages should be delayed for, in seconds. Must be a positive integer",
}
})
.conflicts('delivery-delay', 'no-delivery-delay')
Expand All @@ -38,7 +38,7 @@ function createBody(args: StrictYargsOptionsToInterface<typeof options>): Create
// Workaround, Yargs does not play nicely with both --parameter and --no-parameter set.
// Negating a number parameter returns 0, making deliveryDelay an array with [0, <value>]
if(Array.isArray(args.deliveryDelay)) {
throw new UserError(`Error: can't use --no-delivery-delay with --delivery-delay`);
throw new CommandLineArgsError(`Error: can't use more than a delay setting.`);
}

if(args.deliveryDelay != undefined) {
Expand Down
1 change: 1 addition & 0 deletions packages/wrangler/src/queues/cli/commands/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { handler as createHandler, options as createOptions } from "./create";
import { handler as deleteHandler, options as deleteOptions } from "./delete";
import { handler as listHandler, options as listOptions } from "./list";
import type { CommonYargsArgv } from "../../../yargs-types";
import { CommandLineArgsError, printWranglerBanner } from "../../../index";

export function queues(yargs: CommonYargsArgv) {
yargs.command("list", "List Queues", listOptions, listHandler);
Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/src/queues/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const HandleUnauthorizedError = async (_msg: string, err: Error) => {
);
}
}
throw err;
return err;
};

export function handleFetchError(e: {code?: number}): void {
Expand Down

0 comments on commit 68b9d1b

Please sign in to comment.