Skip to content

Commit 0a75f73

Browse files
Introduce internal isWorkerNotFoundError utility and avoid worker-not-found error code magic numbers in wrangler
1 parent 59534ba commit 0a75f73

File tree

4 files changed

+48
-18
lines changed

4 files changed

+48
-18
lines changed

packages/wrangler/src/__tests__/secret.test.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import { http, HttpResponse } from "msw";
55
import * as TOML from "smol-toml";
66
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
77
import { VERSION_NOT_DEPLOYED_ERR_CODE } from "../secret";
8+
import {
9+
WORKER_NOT_FOUND_ERR_CODE,
10+
workerNotFoundErrorMessage,
11+
} from "../utils/worker-not-found-error";
812
import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
913
import { mockConsoleMethods } from "./helpers/mock-console";
1014
import { clearDialogs, mockConfirm, mockPrompt } from "./helpers/mock-dialogs";
@@ -53,8 +57,8 @@ function mockNoWorkerFound(isBulk = false) {
5357
return HttpResponse.json(
5458
createFetchResult(null, false, [
5559
{
56-
code: 10007,
57-
message: "This Worker does not exist on your account.",
60+
code: WORKER_NOT_FOUND_ERR_CODE,
61+
message: workerNotFoundErrorMessage,
5862
},
5963
])
6064
);
@@ -70,8 +74,8 @@ function mockNoWorkerFound(isBulk = false) {
7074
return HttpResponse.json(
7175
createFetchResult(null, false, [
7276
{
73-
code: 10007,
74-
message: "This Worker does not exist on your account.",
77+
code: WORKER_NOT_FOUND_ERR_CODE,
78+
message: workerNotFoundErrorMessage,
7579
},
7680
])
7781
);

packages/wrangler/src/secret/index.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { fetchSecrets } from "../utils/fetch-secrets";
2121
import { getLegacyScriptName } from "../utils/getLegacyScriptName";
2222
import { readFromStdin, trimTrailingWhitespace } from "../utils/std";
2323
import { useServiceEnvironments } from "../utils/useServiceEnvironments";
24+
import { isWorkerNotFoundError } from "../utils/worker-not-found-error";
2425
import type { Config, WorkerMetadataBinding } from "@cloudflare/workers-utils";
2526

2627
export const VERSION_NOT_DEPLOYED_ERR_CODE = 10215;
@@ -38,14 +39,6 @@ type InheritBindingUpload = {
3839

3940
type SecretBindingRedacted = Omit<SecretBindingUpload, "text">;
4041

41-
function isMissingWorkerError(e: unknown): e is { code: 10007 } {
42-
return (
43-
typeof e === "object" &&
44-
e !== null &&
45-
(e as { code: number }).code === 10007
46-
);
47-
}
48-
4942
async function createDraftWorker({
5043
config,
5144
args,
@@ -239,7 +232,7 @@ export const secretPutCommand = createCommand({
239232
sendMetrics: config.send_metrics,
240233
});
241234
} catch (e) {
242-
if (isMissingWorkerError(e)) {
235+
if (isWorkerNotFoundError(e)) {
243236
// create a draft worker and try again
244237
const result = await createDraftWorker({
245238
config,
@@ -486,7 +479,7 @@ export const secretBulkCommand = createCommand({
486479
const settings = await getSettings();
487480
existingBindings = settings.bindings;
488481
} catch (e) {
489-
if (isMissingWorkerError(e)) {
482+
if (isWorkerNotFoundError(e)) {
490483
// create a draft worker before patching
491484
const result = await createDraftWorker({
492485
config,
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { APIError } from "@cloudflare/workers-utils";
2+
3+
/**
4+
This is the error code from the Cloudflare API signaling that a worker could not be found on the target account
5+
*/
6+
export const WORKER_NOT_FOUND_ERR_CODE = 10007 as const;
7+
8+
/**
9+
This is the error code from the Cloudflare API signaling that a worker environment (legacy) could not be found on the target account
10+
*/
11+
export const WORKER_LEGACY_ENVIRONMENT_NOT_FOUND_ERR_CODE = 10090 as const;
12+
13+
/**
14+
This is the error message from the Cloudflare API signaling that a worker could not be found on the target account
15+
*/
16+
export const workerNotFoundErrorMessage =
17+
"This Worker does not exist on your account.";
18+
19+
/**
20+
* Given an error from the Cloudflare API discerns wether it is caused by a worker that could not be found on the target account
21+
*
22+
* @param error The error object
23+
* @returns true is the object represents is an APIError for caused by a not found worker, false otherwise
24+
*/
25+
export function isWorkerNotFoundError(error: unknown): boolean {
26+
return (
27+
error instanceof APIError &&
28+
(error.code === WORKER_NOT_FOUND_ERR_CODE ||
29+
error.code === WORKER_LEGACY_ENVIRONMENT_NOT_FOUND_ERR_CODE)
30+
);
31+
}

packages/wrangler/src/versions/deploy.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ import {
77
leftT,
88
spinnerWhile,
99
} from "@cloudflare/cli/interactive";
10-
import { APIError, UserError } from "@cloudflare/workers-utils";
10+
import { UserError } from "@cloudflare/workers-utils";
1111
import { fetchResult } from "../cfetch";
1212
import { createCommand } from "../core/create-command";
1313
import { isNonInteractiveOrCI } from "../is-interactive";
1414
import * as metrics from "../metrics";
1515
import { writeOutput } from "../output";
1616
import { requireAuth } from "../user";
1717
import formatLabelledValues from "../utils/render-labelled-values";
18+
import { isWorkerNotFoundError } from "../utils/worker-not-found-error";
1819
import {
1920
createDeployment,
2021
fetchDeployableVersions,
@@ -263,10 +264,11 @@ export async function confirmLatestDeploymentOverwrite(
263264
});
264265
}
265266
} catch (e) {
266-
const isNotFound = e instanceof APIError && e.code == 10007;
267-
if (!isNotFound) {
268-
throw e;
267+
if (isWorkerNotFoundError(e)) {
268+
return;
269269
}
270+
271+
throw e;
270272
}
271273
return true;
272274
}

0 commit comments

Comments
 (0)