Skip to content

Commit

Permalink
Improve error message when updating secret for a non-deployed latest …
Browse files Browse the repository at this point in the history
…version (#5930)
  • Loading branch information
WalshyDev authored May 28, 2024
1 parent ddf43da commit 57daae0
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/fluffy-carrots-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

chore: improve error message when updating secret for a non-deployed latest version.
45 changes: 45 additions & 0 deletions packages/wrangler/src/__tests__/secret.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import readline from "node:readline";
import * as TOML from "@iarna/toml";
import { http, HttpResponse } from "msw";
import { vi } from "vitest";
import { VERSION_NOT_DEPLOYED_ERR_CODE } from "../secret";
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 @@ -308,6 +309,50 @@ describe("wrangler secret", () => {
});
});
});

it("should error if the latest version is not deployed", async () => {
setIsTTY(true);

const scriptName = "test-script";

msw.use(
http.put(
`*/accounts/:accountId/workers/scripts/:scriptName/secrets`,
async ({ params }) => {
expect(params.accountId).toEqual("some-account-id");
expect(params.scriptName).toEqual(scriptName);

// Return our error
return HttpResponse.json({
success: false,
errors: [
{
code: VERSION_NOT_DEPLOYED_ERR_CODE,
message: "latest is not deployed",
},
],
messages: [],
result: null,
});
},
{ once: true }
)
);

mockPrompt({
text: "Enter a secret value:",
options: { isSecret: true },
result: `hunter2
`,
});

await expect(runWrangler(`secret put secret-name --name ${scriptName}`))
.rejects.toThrowErrorMatchingInlineSnapshot(`
[Error: Secret edit failed. You attempted to modify a secret, but the latest version of your Worker isn't currently deployed. Please ensure that the latest version of your Worker is fully deployed (wrangler versions deploy --x-versions) before modifying secrets. Alternatively, you can use the Cloudflare dashboard to modify secrets and deploy the version.
Note: This limitation will be addressed in an upcoming release.]
`);
});
});

describe("delete", () => {
Expand Down
1 change: 0 additions & 1 deletion packages/wrangler/src/cfetch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,6 @@ function throwFetchError(
// so consumers can use it for specific behaviour
const code = response.errors[0]?.code;
if (code) {
//@ts-expect-error non-standard property on Error
error.code = code;
}
throw error;
Expand Down
2 changes: 2 additions & 0 deletions packages/wrangler/src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ export class ParseError extends UserError implements Message {
// Therefore, allow particular `ParseError`s to be marked `reportable`.
export class APIError extends ParseError {
#status?: number;
code?: number;

constructor({ status, ...rest }: Message & { status?: number }) {
super(rest);
this.name = this.constructor.name;
Expand Down
39 changes: 29 additions & 10 deletions packages/wrangler/src/secret/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from "../index";
import { logger } from "../logger";
import * as metrics from "../metrics";
import { parseJSON, readFileSync } from "../parse";
import { APIError, parseJSON, readFileSync } from "../parse";
import { requireAuth } from "../user";
import type { Config } from "../config";
import type { WorkerMetadataBinding } from "../deployment-bundle/create-worker-upload-form";
Expand All @@ -22,6 +22,8 @@ import type {
StrictYargsOptionsToInterface,
} from "../yargs-types";

export const VERSION_NOT_DEPLOYED_ERR_CODE = 10215;

type SecretBindingUpload = {
type: "secret_text";
name: string;
Expand Down Expand Up @@ -168,15 +170,32 @@ export const secret = (secretYargs: CommonYargsArgv) => {
? `/accounts/${accountId}/workers/scripts/${scriptName}/secrets`
: `/accounts/${accountId}/workers/services/${scriptName}/environments/${args.env}/secrets`;

return await fetchResult(url, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
name: args.key,
text: secretValue,
type: "secret_text",
}),
});
try {
return await fetchResult(url, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
name: args.key,
text: secretValue,
type: "secret_text",
}),
});
} catch (e) {
if (
e instanceof APIError &&
e.code === VERSION_NOT_DEPLOYED_ERR_CODE
) {
throw new UserError(
"Secret edit failed. You attempted to modify a secret, but the latest version of your Worker isn't currently deployed. " +
"Please ensure that the latest version of your Worker is fully deployed " +
"(wrangler versions deploy --x-versions) before modifying secrets. " +
"Alternatively, you can use the Cloudflare dashboard to modify secrets and deploy the version." +
"\n\nNote: This limitation will be addressed in an upcoming release."
);
} else {
throw e;
}
}
}

try {
Expand Down

0 comments on commit 57daae0

Please sign in to comment.