Skip to content

Commit c404ff6

Browse files
pages: Add mechanism to handle API error codes (#3801)
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]>
1 parent 0adccc7 commit c404ff6

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

packages/wrangler/src/__tests__/pages/deploy.test.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { runWrangler } from "../helpers/run-wrangler";
1818
import { normalizeProgressSteps } from "./project-upload.test";
1919
import type { Project, UploadPayloadFile } from "../../pages/types";
2020
import type { RestRequest } from "msw";
21+
import { ApiErrorCodes } from "../../pages/errors";
2122

2223
describe("deployment create", () => {
2324
const std = mockConsoleMethods();
@@ -231,7 +232,7 @@ describe("deployment create", () => {
231232
success: false,
232233
errors: [
233234
{
234-
code: 8000000,
235+
code: ApiErrorCodes.UNKNOWN_ERROR,
235236
message: "Something exploded, please retry",
236237
},
237238
],
@@ -389,7 +390,7 @@ describe("deployment create", () => {
389390
success: false,
390391
errors: [
391392
{
392-
code: 8000000,
393+
code: ApiErrorCodes.UNKNOWN_ERROR,
393394
message: "Something exploded, please retry",
394395
},
395396
],
@@ -523,7 +524,7 @@ describe("deployment create", () => {
523524
success: false,
524525
errors: [
525526
{
526-
code: 8000013,
527+
code: ApiErrorCodes.UNAUTHORIZED,
527528
message: "Authorization failed",
528529
},
529530
],

packages/wrangler/src/api/pages/deploy.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { FatalError } from "../../errors";
88
import { logger } from "../../logger";
99
import { buildFunctions } from "../../pages/buildFunctions";
1010
import {
11+
ApiErrorCodes,
1112
FunctionsNoRoutesError,
1213
getFunctionsNoRoutesWarning,
1314
} from "../../pages/errors";
@@ -379,7 +380,7 @@ export async function deploy({
379380
} catch (e) {
380381
lastErr = e;
381382
if (
382-
(e as { code: number }).code === 8000000 &&
383+
(e as { code: number }).code === ApiErrorCodes.UNKNOWN_ERROR &&
383384
attempts < MAX_DEPLOYMENT_ATTEMPTS
384385
) {
385386
logger.debug("failed:", e, "retrying...");

packages/wrangler/src/pages/errors.ts

+9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ import {
55
} from "./constants";
66
import { RoutesValidationError } from "./functions/routes-validation";
77

8+
/**
9+
* Error codes returned by requests to Pages APIs
10+
*/
11+
/* eslint-disable-next-line no-shadow */
12+
export enum ApiErrorCodes {
13+
UNKNOWN_ERROR = 8000000,
14+
UNAUTHORIZED = 8000013,
15+
}
16+
817
/**
918
* Exit code for `pages functions build` when no routes are found.
1019
*/

packages/wrangler/src/pages/upload.tsx

+10-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import type {
2323
} from "../yargs-types";
2424
import type { UploadPayloadFile } from "./types";
2525
import type { FileContainer } from "./validate";
26+
import { ApiErrorCodes } from "./errors";
2627

2728
type UploadArgs = StrictYargsOptionsToInterface<typeof Options>;
2829

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

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

230-
if ((e as { code: number }).code === 8000013 || isJwtExpired(jwt)) {
231+
if (
232+
(e as { code: number }).code === ApiErrorCodes.UNAUTHORIZED ||
233+
isJwtExpired(jwt)
234+
) {
231235
// Looks like the JWT expired, fetch another one
232236
jwt = await fetchJwt();
233237
}
@@ -289,7 +293,10 @@ export const upload = async (
289293
} catch (e) {
290294
await new Promise((resolvePromise) => setTimeout(resolvePromise, 1000));
291295

292-
if ((e as { code: number }).code === 8000013 || isJwtExpired(jwt)) {
296+
if (
297+
(e as { code: number }).code === ApiErrorCodes.UNAUTHORIZED ||
298+
isJwtExpired(jwt)
299+
) {
293300
// Looks like the JWT expired, fetch another one
294301
jwt = await fetchJwt();
295302
}

0 commit comments

Comments
 (0)