Skip to content

Commit

Permalink
secret:bulk exit 1 on failure (#3704)
Browse files Browse the repository at this point in the history
When 'secret:bulk' has an upload request fail it now sends a exit 1 signal that will allow for error handling in other environments.
  • Loading branch information
JacobMGEvans authored Aug 3, 2023
1 parent ff8603b commit 8e231af
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 23 deletions.
8 changes: 8 additions & 0 deletions .changeset/curly-wombats-suffer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"wrangler": minor
---

secret:bulk exit 1 on failure
Previously `secret"bulk` would only log an error on failure of any of the upload requests.
Now when 'secret:bulk' has an upload request fail it throws an Error which sends an `process.exit(1)` at the root `.catch()` signal.
This will enable error handling in programmatic uses of `secret:bulk`.
58 changes: 37 additions & 21 deletions packages/wrangler/src/__tests__/secret.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -658,46 +658,54 @@ describe("wrangler secret", () => {
)
);

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

expect(std.out).toMatchInlineSnapshot(`
"🌀 Creating the secrets for the Worker \\"script-name\\"
✨ Successfully created secret for key: secret-name-2
✨ Successfully created secret for key: secret-name-4
✨ Successfully created secret for key: secret-name-6
"🌀 Creating the secrets for the Worker \\"script-name\\"
✨ Successfully created secret for key: secret-name-2
✨ Successfully created secret for key: secret-name-4
✨ Successfully created secret for key: secret-name-6
Finished processing secrets JSON file:
✨ 3 secrets successfully uploaded
🚨 4 secrets failed to upload"
`);
Finished processing secrets JSON file:
✨ 3 secrets successfully uploaded
If you think this is a bug then please create an issue at https://github.com/cloudflare/workers-sdk/issues/new/choose"
`);
expect(std.err).toMatchInlineSnapshot(`
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1m🚨 Error uploading secret for key: secret-name-1:[0m
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1muploading secret for key: secret-name-1:[0m
request to
https://api.cloudflare.com/client/v4/accounts/some-account-id/workers/scripts/script-name/secrets
failed, reason: Failed to create secret 1
[31mX [41;31m[[41;97mERROR[41;31m][0m [1m🚨 Error uploading secret for key: secret-name-3:[0m
[31mX [41;31m[[41;97mERROR[41;31m][0m [1muploading secret for key: secret-name-3:[0m
request to
https://api.cloudflare.com/client/v4/accounts/some-account-id/workers/scripts/script-name/secrets
failed, reason: Failed to create secret 3
[31mX [41;31m[[41;97mERROR[41;31m][0m [1m🚨 Error uploading secret for key: secret-name-5:[0m
[31mX [41;31m[[41;97mERROR[41;31m][0m [1muploading secret for key: secret-name-5:[0m
request to
https://api.cloudflare.com/client/v4/accounts/some-account-id/workers/scripts/script-name/secrets
failed, reason: Failed to create secret 5
[31mX [41;31m[[41;97mERROR[41;31m][0m [1m🚨 Error uploading secret for key: secret-name-7:[0m
[31mX [41;31m[[41;97mERROR[41;31m][0m [1muploading secret for key: secret-name-7:[0m
request to
https://api.cloudflare.com/client/v4/accounts/some-account-id/workers/scripts/script-name/secrets
failed, reason: Failed to create secret 7
X [ERROR] 🚨 4 secrets failed to upload
"
`);
});
Expand Down Expand Up @@ -725,29 +733,37 @@ describe("wrangler secret", () => {
)
);

await runWrangler("secret:bulk ./secret.json --name script-name");
await expect(async () => {
await runWrangler("secret:bulk ./secret.json --name script-name");
}).rejects.toThrowErrorMatchingInlineSnapshot(
`"🚨 2 secrets failed to upload"`
);

expect(std.out).toMatchInlineSnapshot(`
"🌀 Creating the secrets for the Worker \\"script-name\\"
"🌀 Creating the secrets for the Worker \\"script-name\\"
Finished processing secrets JSON file:
✨ 0 secrets successfully uploaded
🚨 2 secrets failed to upload"
`);
Finished processing secrets JSON file:
✨ 0 secrets successfully uploaded
If you think this is a bug then please create an issue at https://github.com/cloudflare/workers-sdk/issues/new/choose"
`);
expect(std.err).toMatchInlineSnapshot(`
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1m🚨 Error uploading secret for key: secret-name-1:[0m
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1muploading secret for key: secret-name-1:[0m
request to
https://api.cloudflare.com/client/v4/accounts/some-account-id/workers/scripts/script-name/secrets
failed, reason: Failed to create secret 1
[31mX [41;31m[[41;97mERROR[41;31m][0m [1m🚨 Error uploading secret for key: secret-name-2:[0m
[31mX [41;31m[[41;97mERROR[41;31m][0m [1muploading secret for key: secret-name-2:[0m
request to
https://api.cloudflare.com/client/v4/accounts/some-account-id/workers/scripts/script-name/secrets
failed, reason: Failed to create secret 2
X [ERROR] 🚨 2 secrets failed to upload
"
`);
});
Expand Down
5 changes: 3 additions & 2 deletions packages/wrangler/src/secret/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -382,19 +382,20 @@ export const secretBulkHandler = async (secretBulkArgs: SecretBulkArgs) => {
})
.catch((e) => {
logger.error(
`🚨 Error uploading secret for key: ${key}:
`uploading secret for key: ${key}:
${e.message}`
);
return false;
});
})
);

const successes = bulkOutcomes.filter((outcome) => outcome).length;
const failures = bulkOutcomes.length - successes;
logger.log("");
logger.log("Finished processing secrets JSON file:");
logger.log(`✨ ${successes} secrets successfully uploaded`);
if (failures > 0) {
logger.log(`🚨 ${failures} secrets failed to upload`);
throw new Error(`🚨 ${failures} secrets failed to upload`);
}
};

0 comments on commit 8e231af

Please sign in to comment.