Skip to content

Commit

Permalink
pages: Add mechanism to handle API error codes (#3801)
Browse files Browse the repository at this point in the history
Currently, Pages specific API request error codes
are spread all over the codebase, without any
uniform mechanism to handle them. This leads to
error codes that are unreadable and difficult to
keep track of.

This commit introduces a very simple and straightforward
mechanism (by means of a top level `enum`) that handles
and makes such API error codes easier to reason about.

Co-authored-by: Carmen Popoviciu <[email protected]>
  • Loading branch information
CarmenPopoviciu and CarmenPopoviciu authored Aug 22, 2023
1 parent 0adccc7 commit c404ff6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 7 deletions.
7 changes: 4 additions & 3 deletions packages/wrangler/src/__tests__/pages/deploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { runWrangler } from "../helpers/run-wrangler";
import { normalizeProgressSteps } from "./project-upload.test";
import type { Project, UploadPayloadFile } from "../../pages/types";
import type { RestRequest } from "msw";
import { ApiErrorCodes } from "../../pages/errors";

describe("deployment create", () => {
const std = mockConsoleMethods();
Expand Down Expand Up @@ -231,7 +232,7 @@ describe("deployment create", () => {
success: false,
errors: [
{
code: 8000000,
code: ApiErrorCodes.UNKNOWN_ERROR,
message: "Something exploded, please retry",
},
],
Expand Down Expand Up @@ -389,7 +390,7 @@ describe("deployment create", () => {
success: false,
errors: [
{
code: 8000000,
code: ApiErrorCodes.UNKNOWN_ERROR,
message: "Something exploded, please retry",
},
],
Expand Down Expand Up @@ -523,7 +524,7 @@ describe("deployment create", () => {
success: false,
errors: [
{
code: 8000013,
code: ApiErrorCodes.UNAUTHORIZED,
message: "Authorization failed",
},
],
Expand Down
3 changes: 2 additions & 1 deletion packages/wrangler/src/api/pages/deploy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { FatalError } from "../../errors";
import { logger } from "../../logger";
import { buildFunctions } from "../../pages/buildFunctions";
import {
ApiErrorCodes,
FunctionsNoRoutesError,
getFunctionsNoRoutesWarning,
} from "../../pages/errors";
Expand Down Expand Up @@ -379,7 +380,7 @@ export async function deploy({
} catch (e) {
lastErr = e;
if (
(e as { code: number }).code === 8000000 &&
(e as { code: number }).code === ApiErrorCodes.UNKNOWN_ERROR &&
attempts < MAX_DEPLOYMENT_ATTEMPTS
) {
logger.debug("failed:", e, "retrying...");
Expand Down
9 changes: 9 additions & 0 deletions packages/wrangler/src/pages/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ import {
} from "./constants";
import { RoutesValidationError } from "./functions/routes-validation";

/**
* Error codes returned by requests to Pages APIs
*/
/* eslint-disable-next-line no-shadow */
export enum ApiErrorCodes {
UNKNOWN_ERROR = 8000000,
UNAUTHORIZED = 8000013,
}

/**
* Exit code for `pages functions build` when no routes are found.
*/
Expand Down
13 changes: 10 additions & 3 deletions packages/wrangler/src/pages/upload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import type {
} from "../yargs-types";
import type { UploadPayloadFile } from "./types";
import type { FileContainer } from "./validate";
import { ApiErrorCodes } from "./errors";

type UploadArgs = StrictYargsOptionsToInterface<typeof Options>;

Expand Down Expand Up @@ -131,7 +132,7 @@ export const upload = async (
setTimeout(resolvePromise, Math.pow(2, attempts++) * 1000)
);

if ((e as { code: number }).code === 8000013) {
if ((e as { code: number }).code === ApiErrorCodes.UNAUTHORIZED) {
// Looks like the JWT expired, fetch another one
jwt = await fetchJwt();
}
Expand Down Expand Up @@ -227,7 +228,10 @@ export const upload = async (
setTimeout(resolvePromise, Math.pow(2, attempts++) * 1000)
);

if ((e as { code: number }).code === 8000013 || isJwtExpired(jwt)) {
if (
(e as { code: number }).code === ApiErrorCodes.UNAUTHORIZED ||
isJwtExpired(jwt)
) {
// Looks like the JWT expired, fetch another one
jwt = await fetchJwt();
}
Expand Down Expand Up @@ -289,7 +293,10 @@ export const upload = async (
} catch (e) {
await new Promise((resolvePromise) => setTimeout(resolvePromise, 1000));

if ((e as { code: number }).code === 8000013 || isJwtExpired(jwt)) {
if (
(e as { code: number }).code === ApiErrorCodes.UNAUTHORIZED ||
isJwtExpired(jwt)
) {
// Looks like the JWT expired, fetch another one
jwt = await fetchJwt();
}
Expand Down

0 comments on commit c404ff6

Please sign in to comment.