Skip to content

Commit

Permalink
Log token info after authentication errors (#3926)
Browse files Browse the repository at this point in the history
* Fix #1197

* Add changeset

* tests
  • Loading branch information
penalosa authored Oct 2, 2023
1 parent 9a7559b commit f585f69
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changeset/orange-berries-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

Log more detail about tokens after authentication errors
47 changes: 40 additions & 7 deletions packages/wrangler/src/__tests__/deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import {
mswSuccessDeployments,
mswSuccessDeploymentScriptMetadata,
mswSuccessDeploymentScriptAPI,
mswSuccessOauthHandlers,
mswSuccessUserHandlers,
} from "./helpers/msw";
import { FileReaderSync } from "./helpers/msw/read-file-sync";
import { runInTempDir } from "./helpers/run-in-tmp";
Expand Down Expand Up @@ -373,12 +375,20 @@ describe("deploy", () => {
writeWorkerSource();
mockSubDomainRequest();
mockUploadWorkerRequest();
mockConfirm({
text: "Would you like to continue?",
result: false,
});

await runWrangler("deploy ./index");

expect(std.warn).toMatch(
/You are about to publish a Workers Service that was last updated via the script API/
);
expect(std.warn).toMatchInlineSnapshot(`
"▲ [WARNING] You are about to publish a Workers Service that was last updated via the script API.
Edits that have been made via the script API will be overridden by your local code and config.
"
`);
});
});

Expand Down Expand Up @@ -1079,6 +1089,8 @@ Update them to point to this script instead?`,
],
});
writeWorkerSource();
mockServiceScriptData({});

await expect(
runWrangler("deploy ./index")
).rejects.toThrowErrorMatchingInlineSnapshot(
Expand Down Expand Up @@ -8123,7 +8135,7 @@ export default{
writeWranglerToml();
mockSubDomainRequest();
mockUploadWorkerRequest();

msw.use(...mswSuccessOauthHandlers, ...mswSuccessUserHandlers);
msw.use(
rest.get(
"*/accounts/:accountId/workers/services/:scriptName",
Expand Down Expand Up @@ -8151,10 +8163,31 @@ export default{
)
);

await runWrangler("deploy index.js");
expect(std.err).toContain(
`A request to the Cloudflare API (/accounts/some-account-id/workers/services/test-name) failed`
await expect(
runWrangler("deploy index.js")
).rejects.toThrowErrorMatchingInlineSnapshot(
`"A request to the Cloudflare API (/accounts/some-account-id/workers/services/test-name) failed."`
);
expect(std.out).toMatchInlineSnapshot(`
"
X [ERROR] A request to the Cloudflare API (/accounts/some-account-id/workers/services/test-name) failed.
Authentication error [code: 10000]
Getting User settings...
👋 You are logged in with an API Token, associated with the email user@example.com!
┌───────────────┬────────────┐
│ Account Name │ Account ID │
├───────────────┼────────────┤
│ Account One │ account-1 │
├───────────────┼────────────┤
│ Account Two │ account-2 │
├───────────────┼────────────┤
│ Account Three │ account-3 │
└───────────────┴────────────┘
🔓 To see token permissions visit https://dash.cloudflare.com/profile/api-tokens"
`);
});

describe("queues", () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/wrangler/src/deploy/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ export default async function deploy(props: Props): Promise<void> {
// 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) {
logger.error(e);
throw e;
}
}
}
Expand Down Expand Up @@ -1007,7 +1007,7 @@ async function publishRoutesFallback(
return deployedRoutes;
}

function isAuthenticationError(e: unknown): e is ParseError {
export function isAuthenticationError(e: unknown): e is ParseError {
return e instanceof ParseError && (e as { code?: number }).code === 10000;
}

Expand Down
4 changes: 4 additions & 0 deletions packages/wrangler/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { constellation } from "./constellation";
import { d1 } from "./d1";
import { deleteHandler, deleteOptions } from "./delete";
import { deployOptions, deployHandler } from "./deploy";
import { isAuthenticationError } from "./deploy/deploy";
import { isBuildFailure } from "./deployment-bundle/bundle";
import {
deployments,
Expand Down Expand Up @@ -747,6 +748,9 @@ export async function main(argv: string[]): Promise<void> {
// The workaround is to re-run the parsing with an additional `--help` flag, which will result in the correct help message being displayed.
// The `wrangler` object is "frozen"; we cannot reuse that with different args, so we must create a new CLI parser to generate the help message.
await createCLIParser([...argv, "--help"]).parse();
} else if (isAuthenticationError(e)) {
logger.log(formatMessage(e));
await whoami();
} else if (e instanceof ParseError) {
e.notes.push({
text: "\nIf you think this is a bug, please open an issue at: https://github.com/cloudflare/workers-sdk/issues/new/choose",
Expand Down

0 comments on commit f585f69

Please sign in to comment.