Skip to content

Commit

Permalink
fix: throw helpful error if email validation required (#4592)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbbot committed Jan 2, 2024
1 parent c370026 commit 20da658
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 5 deletions.
7 changes: 7 additions & 0 deletions .changeset/serious-seals-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": patch
---

fix: throw helpful error if email validation required

Previously, Wrangler would display the raw API error message and code if email validation was required during `wrangler deploy`. This change ensures a helpful error message is displayed instead, prompting users to check their emails or visit the dashboard for a verification link.
34 changes: 34 additions & 0 deletions packages/wrangler/src/__tests__/deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4046,6 +4046,40 @@ addEventListener('fetch', event => {});`
expect(std.err).toMatchInlineSnapshot(`""`);
});

it("should fail to deploy to the workers.dev domain if email is unverified", async () => {
writeWranglerToml({ workers_dev: true });
writeWorkerSource();
mockUploadWorkerRequest({ available_on_subdomain: false });
mockSubDomainRequest();
msw.use(
rest.post(
`*/accounts/:accountId/workers/scripts/:scriptName/subdomain`,
async (req, res, ctx) => {
return res.once(
ctx.json(
createFetchResult(null, /* success */ false, [
{
code: 10034,
message: "workers.api.error.email_verification_required",
},
])
)
);
}
)
);

await expect(runWrangler("deploy ./index")).rejects.toMatchObject({
text: "Please verify your account's email address and try again.",
notes: [
{
text: "Check your email for a verification link, or login to https://dash.cloudflare.com and request a new one.",
},
{},
],
});
});

it("should offer to create a new workers.dev subdomain when publishing to workers_dev without one", async () => {
writeWranglerToml({
workers_dev: true,
Expand Down
23 changes: 23 additions & 0 deletions packages/wrangler/src/cfetch/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ParseError } from "../parse";

export interface FetchError {
code: number;
message: string;
error_chain?: FetchError[];
}

function buildDetailedError(message: string, ...extra: string[]) {
return new ParseError({
text: message,
notes: extra.map((text) => ({ text })),
});
}

export function maybeThrowFriendlyError(error: FetchError) {
if (error.message === "workers.api.error.email_verification_required") {
throw buildDetailedError(
"Please verify your account's email address and try again.",
"Check your email for a verification link, or login to https://dash.cloudflare.com and request a new one."
);
}
}
10 changes: 5 additions & 5 deletions packages/wrangler/src/cfetch/index.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import { URLSearchParams } from "node:url";
import { logger } from "../logger";
import { ParseError } from "../parse";
import { maybeThrowFriendlyError } from "./errors";
import { fetchInternal, performApiFetch } from "./internal";
import type { FetchError } from "./errors";
import type { RequestInit } from "undici";

// Check out https://api.cloudflare.com/ for API docs.

export interface FetchError {
code: number;
message: string;
error_chain?: FetchError[];
}
export type { FetchError };
export interface FetchResult<ResponseType = unknown> {
success: boolean;
result: ResponseType;
Expand Down Expand Up @@ -164,6 +162,8 @@ function throwFetchError(
resource: string,
response: FetchResult<unknown>
): never {
for (const error of response.errors) maybeThrowFriendlyError(error);

const error = new ParseError({
text: `A request to the Cloudflare API (${resource}) failed.`,
notes: [
Expand Down

0 comments on commit 20da658

Please sign in to comment.