Skip to content

Commit 7344344

Browse files
authored
wrangler containers: Delete should have better error handling (#9268)
1 parent 9707408 commit 7344344

File tree

3 files changed

+131
-14
lines changed

3 files changed

+131
-14
lines changed

.changeset/curvy-hairs-attend.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
`wrangler containers delete` handles API errors correctly

packages/wrangler/src/__tests__/containers/delete.test.ts

Lines changed: 95 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ import { http, HttpResponse } from "msw";
22
import patchConsole from "patch-console";
33
import { mockAccount, setWranglerConfig } from "../cloudchamber/utils";
44
import { mockAccountId, mockApiToken } from "../helpers/mock-account-id";
5-
import { mockConsoleMethods } from "../helpers/mock-console";
5+
import { mockCLIOutput, mockConsoleMethods } from "../helpers/mock-console";
66
import { useMockIsTTY } from "../helpers/mock-istty";
77
import { msw } from "../helpers/msw";
88
import { runWrangler } from "../helpers/run-wrangler";
99

1010
describe("containers delete", () => {
11+
const stdCli = mockCLIOutput();
12+
1113
const std = mockConsoleMethods();
1214
const { setIsTTY } = useMockIsTTY();
1315

@@ -43,6 +45,98 @@ describe("containers delete", () => {
4345
`);
4446
});
4547

48+
async function testStatusCode(code: number) {
49+
setWranglerConfig({});
50+
msw.use(
51+
http.delete(
52+
"*/applications/:id",
53+
async ({ request }) => {
54+
expect(await request.text()).toEqual("");
55+
return new HttpResponse(`{"error": "something happened"}`, {
56+
status: code,
57+
});
58+
},
59+
{ once: true }
60+
)
61+
);
62+
await expect(runWrangler("containers delete 123")).rejects
63+
.toMatchInlineSnapshot(`
64+
[Error: There has been an error deleting the container.
65+
something happened]
66+
`);
67+
expect(stdCli.stderr).toMatchInlineSnapshot(`""`);
68+
expect(stdCli.stdout).toMatchInlineSnapshot(`
69+
"├ Loading account
70+
71+
├ Loading account
72+
73+
╭ Delete your container
74+
75+
"
76+
`);
77+
}
78+
79+
it("should delete container with 400", () => testStatusCode(400));
80+
it("should delete container with 404", () => testStatusCode(404));
81+
82+
it("should delete container with 500", async () => {
83+
setWranglerConfig({});
84+
msw.use(
85+
http.delete(
86+
"*/applications/:id",
87+
async ({ request }) => {
88+
expect(await request.text()).toEqual("");
89+
return new HttpResponse(`{"error": "something happened"}`, {
90+
status: 500,
91+
});
92+
},
93+
{ once: true }
94+
)
95+
);
96+
await expect(runWrangler("containers delete 123")).rejects
97+
.toMatchInlineSnapshot(`
98+
[Error: There has been an unknown error deleting the container.
99+
"{/"error/": /"something happened/"}"]
100+
`);
101+
expect(stdCli.stderr).toMatchInlineSnapshot(`""`);
102+
expect(stdCli.stdout).toMatchInlineSnapshot(`
103+
"├ Loading account
104+
105+
├ Loading account
106+
107+
╭ Delete your container
108+
109+
"
110+
`);
111+
});
112+
113+
it("should delete container", async () => {
114+
setWranglerConfig({});
115+
msw.use(
116+
http.delete(
117+
"*/applications/:id",
118+
async ({ request }) => {
119+
expect(await request.text()).toEqual("");
120+
return new HttpResponse("{}");
121+
},
122+
{ once: true }
123+
)
124+
);
125+
await runWrangler("containers delete 123");
126+
expect(stdCli.stderr).toMatchInlineSnapshot(`""`);
127+
expect(stdCli.stdout).toMatchInlineSnapshot(`
128+
"├ Loading account
129+
130+
├ Loading account
131+
132+
╭ Delete your container
133+
134+
╰ Your container has been deleted
135+
136+
"
137+
`);
138+
});
139+
46140
it("should delete container (json)", async () => {
47141
setIsTTY(false);
48142
setWranglerConfig({});

packages/wrangler/src/containers/containers.ts

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
import { processArgument } from "@cloudflare/cli/args";
99
import { dim, gray } from "@cloudflare/cli/colors";
1010
import { inputPrompt, spinner } from "@cloudflare/cli/interactive";
11-
import { ApplicationsService } from "../cloudchamber/client";
11+
import { ApiError, ApplicationsService } from "../cloudchamber/client";
1212
import { loadAccountSpinner } from "../cloudchamber/common";
1313
import { wrap } from "../cloudchamber/helpers/wrap";
1414
import { UserError } from "../errors";
@@ -38,33 +38,51 @@ export async function deleteCommand(
3838
"You must provide an ID. Use 'wrangler containers list` to view your containers."
3939
);
4040
}
41-
if (deleteArgs.json || !isInteractive()) {
41+
42+
if (deleteArgs.json) {
4243
const container = await ApplicationsService.deleteApplication(
4344
deleteArgs.ID
4445
);
4546
console.log(JSON.stringify(container, null, 4));
4647
return;
4748
}
49+
4850
startSection("Delete your container");
49-
const yes = await inputPrompt({
50-
question:
51-
"Are you sure that you want to delete these containers? The associated DO container will lose access to the containers.",
52-
type: "confirm",
53-
label: "",
54-
});
55-
if (!yes) {
56-
cancel("The operation has been cancelled");
57-
return;
51+
if (isInteractive()) {
52+
const yes = await inputPrompt({
53+
question:
54+
"Are you sure that you want to delete these containers? The associated DO container will lose access to the containers.",
55+
type: "confirm",
56+
label: "",
57+
});
58+
if (!yes) {
59+
cancel("The operation has been cancelled");
60+
return;
61+
}
5862
}
5963

6064
const [, err] = await wrap(
6165
ApplicationsService.deleteApplication(deleteArgs.ID)
6266
);
6367
if (err) {
64-
throw new UserError(
65-
`There has been an internal error deleting your containers.\n ${err.message}`
68+
if (err instanceof ApiError) {
69+
if (err.status === 400 || err.status === 404) {
70+
const body = JSON.parse(err.body);
71+
throw new UserError(
72+
`There has been an error deleting the container.\n${body.error}`
73+
);
74+
}
75+
76+
throw new Error(
77+
`There has been an unknown error deleting the container.\n${JSON.stringify(err.body)}`
78+
);
79+
}
80+
81+
throw new Error(
82+
`There has been an internal error deleting your containers.\n${err.message}`
6683
);
6784
}
85+
6886
endSection("Your container has been deleted");
6987
}
7088

0 commit comments

Comments
 (0)