Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log token info after authentication errors #3926

Merged
merged 3 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -8105,7 +8117,7 @@ export default{
writeWranglerToml();
mockSubDomainRequest();
mockUploadWorkerRequest();

msw.use(...mswSuccessOauthHandlers, ...mswSuccessUserHandlers);
msw.use(
rest.get(
"*/accounts/:accountId/workers/services/:scriptName",
Expand Down Expand Up @@ -8133,10 +8145,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 @@ -1001,7 +1001,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 @@ -10,6 +10,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 @@ -721,6 +722,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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this return or throw?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's thrown at the end of this function

} 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