Skip to content

Commit 199caa4

Browse files
authoredApr 2, 2025··
fix: return actual error in wrangler secrets bulk (#8757)
* actually throw error * mock network error with httpresponse.error()
1 parent 245cfbd commit 199caa4

File tree

5 files changed

+148
-41
lines changed

5 files changed

+148
-41
lines changed
 

‎.changeset/easy-pens-lie.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
fix: return actual error on `wrangler secret bulk`

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

+65-18
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,12 @@ import { clearDialogs, mockConfirm, mockPrompt } from "../helpers/mock-dialogs";
88
import { useMockIsTTY } from "../helpers/mock-istty";
99
import { mockGetMembershipsFail } from "../helpers/mock-oauth-flow";
1010
import { useMockStdin } from "../helpers/mock-stdin";
11-
import { msw } from "../helpers/msw";
11+
import { createFetchResult, msw } from "../helpers/msw";
1212
import { runInTempDir } from "../helpers/run-in-tmp";
1313
import { runWrangler } from "../helpers/run-wrangler";
1414
import type { PagesProject } from "../../pages/download-config";
1515
import type { Interface } from "node:readline";
1616

17-
function createFetchResult(result: unknown, success = true) {
18-
return {
19-
success,
20-
errors: [],
21-
messages: [],
22-
result,
23-
};
24-
}
25-
2617
export function mockGetMemberships(
2718
accounts: { id: string; account: { id: string; name: string } }[]
2819
) {
@@ -680,7 +671,7 @@ describe("wrangler pages secret", () => {
680671
http.patch(
681672
"*/accounts/:accountId/pages/projects/:project",
682673
async () => {
683-
return HttpResponse.json(null);
674+
return HttpResponse.error();
684675
}
685676
)
686677
);
@@ -690,20 +681,76 @@ describe("wrangler pages secret", () => {
690681
"pages secret bulk ./secret.json --project some-project-name"
691682
)
692683
).rejects.toThrowErrorMatchingInlineSnapshot(
693-
`[Error: 🚨 7 secrets failed to upload]`
684+
`[TypeError: Failed to fetch]`
694685
);
695686

696687
expect(std.out).toMatchInlineSnapshot(`
697688
"🌀 Creating the secrets for the Pages project \\"some-project-name\\" (production)
698-
Finished processing secrets file:
699-
✨ 0 secrets successfully uploaded
700-
"
689+
🚨 Secrets failed to upload
690+
691+
[32mIf you think this is a bug then please create an issue at https://github.com/cloudflare/workers-sdk/issues/new/choose[0m"
701692
`);
702693
expect(std.err).toMatchInlineSnapshot(`
703-
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1m🚨 7 secrets failed to upload[0m
694+
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1mFailed to fetch[0m
704695
705-
"
706-
`);
696+
"
697+
`);
698+
});
699+
700+
it("throws a meaningful error", async () => {
701+
writeFileSync(
702+
"secret.json",
703+
JSON.stringify({
704+
"secret-name-1": "secret_text",
705+
"secret-name-2": "secret_text",
706+
})
707+
);
708+
709+
msw.use(
710+
http.get(
711+
`*/accounts/:accountId/workers/scripts/:scriptName/settings`,
712+
({ params }) => {
713+
expect(params.accountId).toEqual("some-account-id");
714+
715+
return HttpResponse.json(createFetchResult({ bindings: [] }));
716+
}
717+
),
718+
http.patch(
719+
`*/accounts/:accountId/workers/scripts/:scriptName/settings`,
720+
({ params }) => {
721+
expect(params.accountId).toEqual("some-account-id");
722+
return HttpResponse.json(
723+
createFetchResult(null, false, [
724+
{
725+
message: "This is a helpful error",
726+
code: 1,
727+
},
728+
])
729+
);
730+
}
731+
)
732+
);
733+
734+
await expect(async () => {
735+
await runWrangler("secret bulk ./secret.json --name script-name");
736+
}).rejects.toThrowErrorMatchingInlineSnapshot(
737+
`[APIError: A request to the Cloudflare API (/accounts/some-account-id/workers/scripts/script-name/settings) failed.]`
738+
);
739+
740+
expect(std.out).toMatchInlineSnapshot(`
741+
"🌀 Creating the secrets for the Worker \\"script-name\\"
742+
743+
🚨 Secrets failed to upload
744+
745+
X [ERROR] A request to the Cloudflare API (/accounts/some-account-id/workers/scripts/script-name/settings) failed.
746+
747+
This is a helpful error [code: 1]
748+
749+
If you think this is a bug, please open an issue at:
750+
https://github.com/cloudflare/workers-sdk/issues/new/choose
751+
752+
"
753+
`);
707754
});
708755
});
709756
});

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

+74-15
Original file line numberDiff line numberDiff line change
@@ -820,9 +820,10 @@ describe("wrangler secret", () => {
820820
`*/accounts/:accountId/workers/scripts/:scriptName/settings`,
821821
({ params }) => {
822822
expect(params.accountId).toEqual("some-account-id");
823-
return HttpResponse.json(
824-
returnNetworkError ? null : createFetchResult(null)
825-
);
823+
if (returnNetworkError) {
824+
return HttpResponse.error();
825+
}
826+
return HttpResponse.json(createFetchResult(null));
826827
}
827828
)
828829
);
@@ -994,7 +995,7 @@ describe("wrangler secret", () => {
994995
await expect(async () => {
995996
await runWrangler("secret bulk ./secret.json --name script-name");
996997
}).rejects.toThrowErrorMatchingInlineSnapshot(
997-
`[Error: 🚨 7 secrets failed to upload]`
998+
`[TypeError: Failed to fetch]`
998999
);
9991000

10001001
expect(std.out).toMatchInlineSnapshot(`
@@ -1004,16 +1005,15 @@ describe("wrangler secret", () => {
10041005
10051006
🌀 Creating the secrets for the Worker \\"script-name\\"
10061007
1007-
Finished processing secrets JSON file:
1008-
✨ 0 secrets successfully uploaded
1008+
🚨 Secrets failed to upload
10091009
10101010
If you think this is a bug then please create an issue at https://github.com/cloudflare/workers-sdk/issues/new/choose"
10111011
`);
10121012
expect(std.err).toMatchInlineSnapshot(`
1013-
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1m🚨 7 secrets failed to upload[0m
1013+
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1mFailed to fetch[0m
10141014
1015-
"
1016-
`);
1015+
"
1016+
`);
10171017
expect(std.warn).toMatchInlineSnapshot(`""`);
10181018
});
10191019

@@ -1030,7 +1030,7 @@ describe("wrangler secret", () => {
10301030
await expect(async () => {
10311031
await runWrangler("secret bulk ./secret.json --name script-name");
10321032
}).rejects.toThrowErrorMatchingInlineSnapshot(
1033-
`[Error: 🚨 2 secrets failed to upload]`
1033+
`[TypeError: Failed to fetch]`
10341034
);
10351035

10361036
expect(std.out).toMatchInlineSnapshot(`
@@ -1040,19 +1040,78 @@ describe("wrangler secret", () => {
10401040
10411041
🌀 Creating the secrets for the Worker \\"script-name\\"
10421042
1043-
Finished processing secrets JSON file:
1044-
✨ 0 secrets successfully uploaded
1043+
🚨 Secrets failed to upload
10451044
10461045
If you think this is a bug then please create an issue at https://github.com/cloudflare/workers-sdk/issues/new/choose"
10471046
`);
10481047
expect(std.err).toMatchInlineSnapshot(`
1049-
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1m🚨 2 secrets failed to upload[0m
1048+
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1mFailed to fetch[0m
10501049
1051-
"
1052-
`);
1050+
"
1051+
`);
10531052
expect(std.warn).toMatchInlineSnapshot(`""`);
10541053
});
10551054

1055+
it("throws a meaningful error", async () => {
1056+
writeFileSync(
1057+
"secret.json",
1058+
JSON.stringify({
1059+
"secret-name-1": "secret_text",
1060+
"secret-name-2": "secret_text",
1061+
})
1062+
);
1063+
1064+
msw.use(
1065+
http.get(
1066+
`*/accounts/:accountId/workers/scripts/:scriptName/settings`,
1067+
({ params }) => {
1068+
expect(params.accountId).toEqual("some-account-id");
1069+
1070+
return HttpResponse.json(createFetchResult({ bindings: [] }));
1071+
}
1072+
),
1073+
http.patch(
1074+
`*/accounts/:accountId/workers/scripts/:scriptName/settings`,
1075+
({ params }) => {
1076+
expect(params.accountId).toEqual("some-account-id");
1077+
return HttpResponse.json(
1078+
createFetchResult(null, false, [
1079+
{
1080+
message: "This is a helpful error",
1081+
code: 1,
1082+
},
1083+
])
1084+
);
1085+
}
1086+
)
1087+
);
1088+
1089+
await expect(async () => {
1090+
await runWrangler("secret bulk ./secret.json --name script-name");
1091+
}).rejects.toThrowErrorMatchingInlineSnapshot(
1092+
`[APIError: A request to the Cloudflare API (/accounts/some-account-id/workers/scripts/script-name/settings) failed.]`
1093+
);
1094+
1095+
expect(std.out).toMatchInlineSnapshot(`
1096+
"
1097+
⛅️ wrangler x.x.x
1098+
------------------
1099+
1100+
🌀 Creating the secrets for the Worker \\"script-name\\"
1101+
1102+
🚨 Secrets failed to upload
1103+
1104+
X [ERROR] A request to the Cloudflare API (/accounts/some-account-id/workers/scripts/script-name/settings) failed.
1105+
1106+
This is a helpful error [code: 1]
1107+
1108+
If you think this is a bug, please open an issue at:
1109+
https://github.com/cloudflare/workers-sdk/issues/new/choose
1110+
1111+
"
1112+
`);
1113+
});
1114+
10561115
it("should merge existing bindings and secrets when patching", async () => {
10571116
writeFileSync(
10581117
"secret.json",

‎packages/wrangler/src/pages/secret/index.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,8 @@ export const secret = (secretYargs: CommonYargsArgv, subHelp: SubHelp) => {
231231
} secrets successfully uploaded`
232232
);
233233
} catch (err) {
234-
logger.log("Finished processing secrets file:");
235-
logger.log(`✨ 0 secrets successfully uploaded`);
236-
throw new FatalError(
237-
`🚨 ${Object.keys(upsertBindings).length} secrets failed to upload`
238-
);
234+
logger.log(`🚨 Secrets failed to upload`);
235+
throw err;
239236
}
240237
}
241238
)

‎packages/wrangler/src/secret/index.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -515,9 +515,8 @@ export const secretBulkCommand = createCommand({
515515
logger.log(`✨ ${upsertBindings.length} secrets successfully uploaded`);
516516
} catch (err) {
517517
logger.log("");
518-
logger.log("Finished processing secrets JSON file:");
519-
logger.log(`✨ 0 secrets successfully uploaded`);
520-
throw new Error(`🚨 ${upsertBindings.length} secrets failed to upload`);
518+
logger.log(`🚨 Secrets failed to upload`);
519+
throw err;
521520
}
522521
},
523522
});

0 commit comments

Comments
 (0)
Please sign in to comment.