Skip to content

Commit 5e6982e

Browse files
committed
debug
1 parent 197dfd5 commit 5e6982e

File tree

5 files changed

+91
-42
lines changed

5 files changed

+91
-42
lines changed

packages/containers-shared/src/client/core/request.ts

Lines changed: 50 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,17 @@ import type { ApiRequestOptions } from "./ApiRequestOptions";
99
import type { ApiResult } from "./ApiResult";
1010
import type { OnCancel } from "./CancelablePromise";
1111

12-
interface FetchResponseInfo {
12+
type FetchResponseInfo = {
1313
code: number;
14-
documentation_url?: string;
1514
message: string;
16-
source?: {
17-
pointer?: string;
18-
};
19-
}
15+
};
2016

21-
interface FetchResult<ResponseType = any> {
17+
type FetchResult<ResponseType = unknown> = {
2218
success: boolean;
23-
result: ResponseType;
24-
errors: FetchResponseInfo[];
19+
result?: ResponseType;
20+
errors?: FetchResponseInfo[];
2521
messages?: FetchResponseInfo[];
26-
}
22+
};
2723

2824
const isDefined = <T>(
2925
value: T | null | undefined
@@ -220,6 +216,36 @@ const isResponseSchemaV4 = (
220216
return config.BASE.endsWith("/containers");
221217
};
222218

219+
const parseResponseSchemaV4 = <T>(
220+
url: string,
221+
response: Response,
222+
responseHeader: string | undefined,
223+
responseBody: any
224+
): ApiResult => {
225+
const fetchResult = responseBody as FetchResult<T>;
226+
const ok = response.ok && fetchResult.success;
227+
console.log(`${JSON.stringify(fetchResult)}`);
228+
let result: T | undefined;
229+
let error: FetchResponseInfo | undefined;
230+
if (ok && fetchResult.result !== undefined) {
231+
result = fetchResult.result;
232+
} else {
233+
if (fetchResult.errors !== undefined) {
234+
error = fetchResult.errors[0];
235+
} else {
236+
console.log("no errors");
237+
}
238+
}
239+
240+
return {
241+
url,
242+
ok,
243+
status: response.status,
244+
statusText: response.statusText,
245+
body: responseHeader ?? ok ? result : error,
246+
};
247+
};
248+
223249
export const sendRequest = async (
224250
config: OpenAPIConfig,
225251
options: ApiRequestOptions,
@@ -342,26 +368,24 @@ export const request = <T>(
342368
options.responseHeader
343369
);
344370

345-
const parseResponseSchemaV4 = (body: any): ApiResult => {
346-
const fetchResult = body as FetchResult<T>;
347-
return {
371+
let result: ApiResult;
372+
373+
if (isResponseSchemaV4(config, options)) {
374+
result = parseResponseSchemaV4(
375+
url,
376+
response,
377+
responseHeader,
378+
responseBody
379+
);
380+
} else {
381+
result = {
348382
url,
349-
ok: response.ok && fetchResult.success,
383+
ok: response.ok,
350384
status: response.status,
351385
statusText: response.statusText,
352-
body: responseHeader ?? fetchResult.result,
386+
body: responseHeader ?? responseBody,
353387
};
354-
};
355-
356-
const result: ApiResult = isResponseSchemaV4(config, options)
357-
? parseResponseSchemaV4(responseBody)
358-
: {
359-
url,
360-
ok: response.ok,
361-
status: response.status,
362-
statusText: response.statusText,
363-
body: responseHeader ?? responseBody,
364-
};
388+
}
365389

366390
catchErrorCodes(options, result);
367391
resolve(result.body);

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

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import { useMockIsTTY } from "../helpers/mock-istty";
77
import { msw } from "../helpers/msw";
88
import { runWrangler } from "../helpers/run-wrangler";
99

10+
const testContainerID = "6925adea-c4ad-4aa6-bffd-d26783e9afbb";
11+
1012
describe("containers delete", () => {
1113
const stdCli = mockCLIOutput();
1214

@@ -52,14 +54,17 @@ describe("containers delete", () => {
5254
"*/applications/:id",
5355
async ({ request }) => {
5456
expect(await request.text()).toEqual("");
55-
return new HttpResponse(`{"error": "something happened"}`, {
56-
status: code,
57-
});
57+
return new HttpResponse(
58+
`{"success": false, "errors": [{"code": 1000, "message": "something happened"}]}`,
59+
{
60+
status: code,
61+
}
62+
);
5863
},
5964
{ once: true }
6065
)
6166
);
62-
await expect(runWrangler("containers delete 123")).rejects
67+
await expect(runWrangler(`containers delete ${testContainerID}`)).rejects
6368
.toMatchInlineSnapshot(`
6469
[Error: There has been an error deleting the container.
6570
something happened]
@@ -86,14 +91,14 @@ describe("containers delete", () => {
8691
"*/applications/:id",
8792
async ({ request }) => {
8893
expect(await request.text()).toEqual("");
89-
return new HttpResponse(`{"error": "something happened"}`, {
94+
return new HttpResponse(`{"success": false, "errors": []}`, {
9095
status: 500,
9196
});
9297
},
9398
{ once: true }
9499
)
95100
);
96-
await expect(runWrangler("containers delete 123")).rejects
101+
await expect(runWrangler(`containers delete ${testContainerID}`)).rejects
97102
.toMatchInlineSnapshot(`
98103
[Error: There has been an unknown error deleting the container.
99104
"{/"error/": /"something happened/"}"]
@@ -122,7 +127,7 @@ describe("containers delete", () => {
122127
{ once: true }
123128
)
124129
);
125-
await runWrangler("containers delete 123");
130+
await runWrangler(`containers delete ${testContainerID}`);
126131
expect(stdCli.stderr).toMatchInlineSnapshot(`""`);
127132
expect(stdCli.stdout).toMatchInlineSnapshot(`
128133
"├ Loading account
@@ -145,12 +150,12 @@ describe("containers delete", () => {
145150
"*/applications/:id",
146151
async ({ request }) => {
147152
expect(await request.text()).toEqual("");
148-
return new HttpResponse("{}");
153+
return new HttpResponse("{success: true, result: {}}");
149154
},
150155
{ once: true }
151156
)
152157
);
153-
await runWrangler("containers delete --json asdf");
158+
await runWrangler(`containers delete --json ${testContainerID}`);
154159
expect(std.err).toMatchInlineSnapshot(`""`);
155160
expect(std.out).toMatchInlineSnapshot(`"\\"{}\\""`);
156161
});
@@ -163,15 +168,21 @@ describe("containers delete", () => {
163168
"*/applications/*",
164169
async ({ request }) => {
165170
expect(await request.text()).toEqual("");
166-
return new HttpResponse(JSON.stringify({ error: "Not Found" }), {
167-
status: 404,
168-
});
171+
return new HttpResponse(
172+
JSON.stringify({
173+
success: false,
174+
errors: [{ code: 1000, message: "Not Found" }],
175+
}),
176+
{
177+
status: 404,
178+
}
179+
);
169180
},
170181
{ once: true }
171182
)
172183
);
173184
expect(std.err).toMatchInlineSnapshot(`""`);
174-
await runWrangler("containers delete --json nope");
185+
await runWrangler(`containers delete --json ${testContainerID}`);
175186
expect(std.out).toMatchInlineSnapshot(
176187
`"\\"{/\\"error/\\":/\\"Not Found/\\"}\\""`
177188
);

packages/wrangler/src/cloudchamber/common.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ export const cloudchamberScope = "cloudchamber:write" as const;
4040

4141
export type CommonCloudchamberConfiguration = { json: boolean };
4242

43+
const containerIdRe = /[^/]{36}/;
44+
45+
export function isValidContainerID(value: string): boolean {
46+
const matches = value.match(containerIdRe);
47+
return matches !== null;
48+
}
49+
4350
/**
4451
* Regular expression for matching an image name.
4552
*

packages/wrangler/src/cloudchamber/delete.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import { inputPrompt } from "@cloudflare/cli/interactive";
33
import { DeploymentsService } from "@cloudflare/containers-shared";
44
import { UserError } from "../errors";
55
import { logDeployment, pickDeployment } from "./cli/deployments";
6-
import { interactWithUser, loadAccountSpinner } from "./common";
6+
import {
7+
interactWithUser,
8+
isValidContainerID,
9+
loadAccountSpinner,
10+
} from "./common";
711
import { wrap } from "./helpers/wrap";
812
import type { Config } from "../config";
913
import type {
@@ -31,6 +35,8 @@ export async function deleteCommand(
3135
throw new Error(
3236
"there needs to be a deploymentId when you can't interact with the wrangler cli"
3337
);
38+
} else if (!isValidContainerID(deleteArgs.deploymentId)) {
39+
throw new UserError("deploymentId must be a UUID");
3440
}
3541

3642
const deployment = await DeploymentsService.deleteDeploymentV2(

packages/wrangler/src/containers/containers.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { processArgument } from "@cloudflare/cli/args";
99
import { dim, gray } from "@cloudflare/cli/colors";
1010
import { inputPrompt, spinner } from "@cloudflare/cli/interactive";
1111
import { ApiError, ApplicationsService } from "@cloudflare/containers-shared";
12-
import { loadAccountSpinner } from "../cloudchamber/common";
12+
import { isValidContainerID, loadAccountSpinner } from "../cloudchamber/common";
1313
import { wrap } from "../cloudchamber/helpers/wrap";
1414
import { UserError } from "../errors";
1515
import isInteractive from "../is-interactive";
@@ -34,11 +34,12 @@ export async function deleteCommand(
3434
deleteArgs: StrictYargsOptionsToInterfaceJSON<typeof deleteYargs>,
3535
_config: Config
3636
) {
37-
await loadAccountSpinner(deleteArgs);
3837
if (!deleteArgs.ID) {
3938
throw new Error(
4039
"You must provide an ID. Use 'wrangler containers list` to view your containers."
4140
);
41+
} else if (!isValidContainerID(deleteArgs.ID)) {
42+
throw new UserError("ID must be a UUID");
4243
}
4344

4445
if (deleteArgs.json) {

0 commit comments

Comments
 (0)