Skip to content

Commit 8e231af

Browse files
authored
secret:bulk exit 1 on failure (#3704)
When 'secret:bulk' has an upload request fail it now sends a exit 1 signal that will allow for error handling in other environments.
1 parent ff8603b commit 8e231af

File tree

3 files changed

+48
-23
lines changed

3 files changed

+48
-23
lines changed

.changeset/curly-wombats-suffer.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"wrangler": minor
3+
---
4+
5+
secret:bulk exit 1 on failure
6+
Previously `secret"bulk` would only log an error on failure of any of the upload requests.
7+
Now when 'secret:bulk' has an upload request fail it throws an Error which sends an `process.exit(1)` at the root `.catch()` signal.
8+
This will enable error handling in programmatic uses of `secret:bulk`.

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

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -658,46 +658,54 @@ describe("wrangler secret", () => {
658658
)
659659
);
660660

661-
await runWrangler("secret:bulk ./secret.json --name script-name");
661+
await expect(async () => {
662+
await runWrangler("secret:bulk ./secret.json --name script-name");
663+
}).rejects.toThrowErrorMatchingInlineSnapshot(
664+
`"🚨 4 secrets failed to upload"`
665+
);
662666

663667
expect(std.out).toMatchInlineSnapshot(`
664-
"🌀 Creating the secrets for the Worker \\"script-name\\"
665-
✨ Successfully created secret for key: secret-name-2
666-
✨ Successfully created secret for key: secret-name-4
667-
✨ Successfully created secret for key: secret-name-6
668+
"🌀 Creating the secrets for the Worker \\"script-name\\"
669+
✨ Successfully created secret for key: secret-name-2
670+
✨ Successfully created secret for key: secret-name-4
671+
✨ Successfully created secret for key: secret-name-6
668672
669-
Finished processing secrets JSON file:
670-
✨ 3 secrets successfully uploaded
671-
🚨 4 secrets failed to upload"
672-
`);
673+
Finished processing secrets JSON file:
674+
✨ 3 secrets successfully uploaded
675+
676+
If you think this is a bug then please create an issue at https://github.com/cloudflare/workers-sdk/issues/new/choose"
677+
`);
673678
expect(std.err).toMatchInlineSnapshot(`
674-
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1m🚨 Error uploading secret for key: secret-name-1:[0m
679+
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1muploading secret for key: secret-name-1:[0m
675680
676681
request to
677682
https://api.cloudflare.com/client/v4/accounts/some-account-id/workers/scripts/script-name/secrets
678683
failed, reason: Failed to create secret 1
679684
680685
681-
[31mX [41;31m[[41;97mERROR[41;31m][0m [1m🚨 Error uploading secret for key: secret-name-3:[0m
686+
[31mX [41;31m[[41;97mERROR[41;31m][0m [1muploading secret for key: secret-name-3:[0m
682687
683688
request to
684689
https://api.cloudflare.com/client/v4/accounts/some-account-id/workers/scripts/script-name/secrets
685690
failed, reason: Failed to create secret 3
686691
687692
688-
[31mX [41;31m[[41;97mERROR[41;31m][0m [1m🚨 Error uploading secret for key: secret-name-5:[0m
693+
[31mX [41;31m[[41;97mERROR[41;31m][0m [1muploading secret for key: secret-name-5:[0m
689694
690695
request to
691696
https://api.cloudflare.com/client/v4/accounts/some-account-id/workers/scripts/script-name/secrets
692697
failed, reason: Failed to create secret 5
693698
694699
695-
[31mX [41;31m[[41;97mERROR[41;31m][0m [1m🚨 Error uploading secret for key: secret-name-7:[0m
700+
[31mX [41;31m[[41;97mERROR[41;31m][0m [1muploading secret for key: secret-name-7:[0m
696701
697702
request to
698703
https://api.cloudflare.com/client/v4/accounts/some-account-id/workers/scripts/script-name/secrets
699704
failed, reason: Failed to create secret 7
700705
706+
707+
X [ERROR] 🚨 4 secrets failed to upload
708+
701709
"
702710
`);
703711
});
@@ -725,29 +733,37 @@ describe("wrangler secret", () => {
725733
)
726734
);
727735

728-
await runWrangler("secret:bulk ./secret.json --name script-name");
736+
await expect(async () => {
737+
await runWrangler("secret:bulk ./secret.json --name script-name");
738+
}).rejects.toThrowErrorMatchingInlineSnapshot(
739+
`"🚨 2 secrets failed to upload"`
740+
);
729741

730742
expect(std.out).toMatchInlineSnapshot(`
731-
"🌀 Creating the secrets for the Worker \\"script-name\\"
743+
"🌀 Creating the secrets for the Worker \\"script-name\\"
732744
733-
Finished processing secrets JSON file:
734-
✨ 0 secrets successfully uploaded
735-
🚨 2 secrets failed to upload"
736-
`);
745+
Finished processing secrets JSON file:
746+
✨ 0 secrets successfully uploaded
747+
748+
If you think this is a bug then please create an issue at https://github.com/cloudflare/workers-sdk/issues/new/choose"
749+
`);
737750
expect(std.err).toMatchInlineSnapshot(`
738-
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1m🚨 Error uploading secret for key: secret-name-1:[0m
751+
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1muploading secret for key: secret-name-1:[0m
739752
740753
request to
741754
https://api.cloudflare.com/client/v4/accounts/some-account-id/workers/scripts/script-name/secrets
742755
failed, reason: Failed to create secret 1
743756
744757
745-
[31mX [41;31m[[41;97mERROR[41;31m][0m [1m🚨 Error uploading secret for key: secret-name-2:[0m
758+
[31mX [41;31m[[41;97mERROR[41;31m][0m [1muploading secret for key: secret-name-2:[0m
746759
747760
request to
748761
https://api.cloudflare.com/client/v4/accounts/some-account-id/workers/scripts/script-name/secrets
749762
failed, reason: Failed to create secret 2
750763
764+
765+
X [ERROR] 🚨 2 secrets failed to upload
766+
751767
"
752768
`);
753769
});

packages/wrangler/src/secret/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -382,19 +382,20 @@ export const secretBulkHandler = async (secretBulkArgs: SecretBulkArgs) => {
382382
})
383383
.catch((e) => {
384384
logger.error(
385-
`🚨 Error uploading secret for key: ${key}:
385+
`uploading secret for key: ${key}:
386386
${e.message}`
387387
);
388388
return false;
389389
});
390390
})
391391
);
392+
392393
const successes = bulkOutcomes.filter((outcome) => outcome).length;
393394
const failures = bulkOutcomes.length - successes;
394395
logger.log("");
395396
logger.log("Finished processing secrets JSON file:");
396397
logger.log(`✨ ${successes} secrets successfully uploaded`);
397398
if (failures > 0) {
398-
logger.log(`🚨 ${failures} secrets failed to upload`);
399+
throw new Error(`🚨 ${failures} secrets failed to upload`);
399400
}
400401
};

0 commit comments

Comments
 (0)