Skip to content
Open
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
12 changes: 8 additions & 4 deletions packages/wrangler/src/__tests__/secret.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import { http, HttpResponse } from "msw";
import * as TOML from "smol-toml";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { VERSION_NOT_DEPLOYED_ERR_CODE } from "../secret";
import {
WORKER_NOT_FOUND_ERR_CODE,
workerNotFoundErrorMessage,
} from "../utils/worker-not-found-error";
import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
import { mockConsoleMethods } from "./helpers/mock-console";
import { clearDialogs, mockConfirm, mockPrompt } from "./helpers/mock-dialogs";
Expand Down Expand Up @@ -53,8 +57,8 @@ function mockNoWorkerFound(isBulk = false) {
return HttpResponse.json(
createFetchResult(null, false, [
{
code: 10007,
message: "This Worker does not exist on your account.",
code: WORKER_NOT_FOUND_ERR_CODE,
message: workerNotFoundErrorMessage,
},
])
);
Expand All @@ -70,8 +74,8 @@ function mockNoWorkerFound(isBulk = false) {
return HttpResponse.json(
createFetchResult(null, false, [
{
code: 10007,
message: "This Worker does not exist on your account.",
code: WORKER_NOT_FOUND_ERR_CODE,
message: workerNotFoundErrorMessage,
},
])
);
Expand Down
9 changes: 4 additions & 5 deletions packages/wrangler/src/deploy/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import { downloadWorkerConfig } from "../utils/download-worker-config";
import { helpIfErrorIsSizeOrScriptStartup } from "../utils/friendly-validator-errors";
import { printBindings } from "../utils/print-bindings";
import { retryOnAPIFailure } from "../utils/retry";
import { isWorkerNotFoundError } from "../utils/worker-not-found-error";
import {
createDeployment,
patchNonVersionedScriptSettings,
Expand Down Expand Up @@ -463,12 +464,10 @@ export default async function deploy(props: Props): Promise<{
}
}
} catch (e) {
// code: 10090, message: workers.api.error.service_not_found
// is thrown from the above fetchResult on the first deploy of a Worker
if ((e as { code?: number }).code !== 10090) {
throw e;
} else {
if (isWorkerNotFoundError(e)) {
workerExists = false;
} else {
throw e;
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions packages/wrangler/src/durable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import assert from "node:assert";
import { configFileName } from "@cloudflare/workers-utils";
import { fetchResult } from "./cfetch";
import { logger } from "./logger";
import { isWorkerNotFoundError } from "./utils/worker-not-found-error";
import type { CfWorkerInit, Config } from "@cloudflare/workers-utils";

/**
Expand Down Expand Up @@ -111,10 +112,8 @@ export async function getMigrationsToUpload(

const suppressNotFoundError = (err: unknown) => {
if (
![
10090, // corresponds to workers.api.error.service_not_found, so the script wasn't previously published at all
10092, // workers.api.error.environment_not_found, so the script wasn't published to this environment yet
].includes((err as { code: number }).code)
!isWorkerNotFoundError(err) &&
(err as { code: number }).code !== 10092 // workers.api.error.environment_not_found, so the script wasn't published to this environment yet
) {
throw err;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/wrangler/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { requireAuth } from "./user";
import { createBatches } from "./utils/create-batches";
import { downloadWorkerConfig } from "./utils/download-worker-config";
import * as shellquote from "./utils/shell-quote";
import { isWorkerNotFoundError } from "./utils/worker-not-found-error";
import type { PackageManager } from "./package-manager";
import type { ServiceMetadataRes } from "@cloudflare/workers-utils";
import type { ReadableStream } from "node:stream/web";
Expand Down Expand Up @@ -83,7 +84,7 @@ export const init = createCommand({
`/accounts/${accountId}/workers/services/${args.fromDash}`
);
} catch (err) {
if ((err as { code?: number }).code === 10090) {
if (isWorkerNotFoundError(err)) {
throw new UserError(
"wrangler couldn't find a Worker with that name in your account.\nRun `wrangler whoami` to confirm you're logged into the correct account.",
{
Expand Down
4 changes: 2 additions & 2 deletions packages/wrangler/src/match-tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import { fetchResult } from "./cfetch";
import { logger } from "./logger";
import { getCloudflareAccountIdFromEnv } from "./user/auth-variables";
import { isWorkerNotFoundError } from "./utils/worker-not-found-error";
import type {
ComplianceConfig,
ServiceMetadataRes,
Expand Down Expand Up @@ -52,8 +53,7 @@ export async function verifyWorkerMatchesCITag(
logger.debug(`API returned with tag: ${tag} for worker: ${workerName}`);
} catch (e) {
logger.debug(e);
// code: 10090, message: workers.api.error.service_not_found
if ((e as { code?: number }).code === 10090) {
if (isWorkerNotFoundError(e)) {
throw new FatalError(
`The name in your ${configFileName(configPath)} file (${workerName}) must match the name of your Worker. Please update the name field in your ${configFileName(configPath)} file.`
);
Expand Down
13 changes: 3 additions & 10 deletions packages/wrangler/src/secret/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { fetchSecrets } from "../utils/fetch-secrets";
import { getLegacyScriptName } from "../utils/getLegacyScriptName";
import { readFromStdin, trimTrailingWhitespace } from "../utils/std";
import { useServiceEnvironments } from "../utils/useServiceEnvironments";
import { isWorkerNotFoundError } from "../utils/worker-not-found-error";
import type { Config, WorkerMetadataBinding } from "@cloudflare/workers-utils";

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

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

function isMissingWorkerError(e: unknown): e is { code: 10007 } {
return (
typeof e === "object" &&
e !== null &&
(e as { code: number }).code === 10007
);
}

async function createDraftWorker({
config,
args,
Expand Down Expand Up @@ -239,7 +232,7 @@ export const secretPutCommand = createCommand({
sendMetrics: config.send_metrics,
});
} catch (e) {
if (isMissingWorkerError(e)) {
if (isWorkerNotFoundError(e)) {
// create a draft worker and try again
const result = await createDraftWorker({
config,
Expand Down Expand Up @@ -486,7 +479,7 @@ export const secretBulkCommand = createCommand({
const settings = await getSettings();
existingBindings = settings.bindings;
} catch (e) {
if (isMissingWorkerError(e)) {
if (isWorkerNotFoundError(e)) {
// create a draft worker before patching
const result = await createDraftWorker({
config,
Expand Down
30 changes: 30 additions & 0 deletions packages/wrangler/src/utils/worker-not-found-error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
This is the error code from the Cloudflare API signaling that a worker could not be found on the target account
*/
export const WORKER_NOT_FOUND_ERR_CODE = 10007 as const;

/**
This is the error code from the Cloudflare API signaling that a worker environment (legacy) could not be found on the target account
*/
export const WORKER_LEGACY_ENVIRONMENT_NOT_FOUND_ERR_CODE = 10090 as const;

/**
This is the error message from the Cloudflare API signaling that a worker could not be found on the target account
*/
export const workerNotFoundErrorMessage =
"This Worker does not exist on your account.";

/**
* Given an error from the Cloudflare API discerns wether it is caused by a worker that could not be found on the target account
*
* @param error The error object
* @returns true is the object represents an error from the Cloudflare API caused by a not found worker, false otherwise
*/
export function isWorkerNotFoundError(error: unknown): boolean {
return (
error instanceof Object &&
"code" in error &&
(error.code === WORKER_NOT_FOUND_ERR_CODE ||
error.code === WORKER_LEGACY_ENVIRONMENT_NOT_FOUND_ERR_CODE)
);
}
6 changes: 3 additions & 3 deletions packages/wrangler/src/versions/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import {
leftT,
spinnerWhile,
} from "@cloudflare/cli/interactive";
import { APIError, UserError } from "@cloudflare/workers-utils";
import { UserError } from "@cloudflare/workers-utils";
import { fetchResult } from "../cfetch";
import { createCommand } from "../core/create-command";
import { isNonInteractiveOrCI } from "../is-interactive";
import * as metrics from "../metrics";
import { writeOutput } from "../output";
import { requireAuth } from "../user";
import formatLabelledValues from "../utils/render-labelled-values";
import { isWorkerNotFoundError } from "../utils/worker-not-found-error";
import {
createDeployment,
fetchDeployableVersions,
Expand Down Expand Up @@ -263,8 +264,7 @@ export async function confirmLatestDeploymentOverwrite(
});
}
} catch (e) {
const isNotFound = e instanceof APIError && e.code == 10007;
if (!isNotFound) {
if (!isWorkerNotFoundError(e)) {
throw e;
}
}
Expand Down
5 changes: 2 additions & 3 deletions packages/wrangler/src/versions/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import { getScriptName } from "../utils/getScriptName";
import { printBindings } from "../utils/print-bindings";
import { retryOnAPIFailure } from "../utils/retry";
import { useServiceEnvironments } from "../utils/useServiceEnvironments";
import { isWorkerNotFoundError } from "../utils/worker-not-found-error";
import { patchNonVersionedScriptSettings } from "./api";
import type { AssetsOptions } from "../assets";
import type { Entry } from "../deployment-bundle/entry";
Expand Down Expand Up @@ -463,9 +464,7 @@ export default async function versionsUpload(props: Props): Promise<{
}
}
} catch (e) {
// code: 10090, message: workers.api.error.service_not_found
// is thrown from the above fetchResult on the first deploy of a Worker
if ((e as { code?: number }).code !== 10090) {
if (!isWorkerNotFoundError(e)) {
throw e;
}
}
Expand Down
Loading