Skip to content

Commit

Permalink
Make the getRequestContext's error message more helpful during loca…
Browse files Browse the repository at this point in the history
…l development (#669)
  • Loading branch information
dario-piotrowicz authored Feb 20, 2024
1 parent 1cdde11 commit 90e140c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
9 changes: 9 additions & 0 deletions .changeset/beige-ducks-glow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@cloudflare/next-on-pages': patch
---

Make the `getRequestContext`'s error message more helpful during local development

During local development add information in the `getRequestContext`'s error
message reminding the user to setup the dev platform via `setupDevPlatform`
in their config file
20 changes: 19 additions & 1 deletion packages/next-on-pages/src/api/getRequestContext.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'server-only';
import dedent from 'dedent-tabs';

declare global {
// eslint-disable-next-line @typescript-eslint/no-empty-interface
Expand Down Expand Up @@ -43,7 +44,24 @@ export function getRequestContext<
>();

if (!cloudflareRequestContext) {
throw new Error('Error: failed to retrieve the Cloudflare request context');
let errorMessage = 'Failed to retrieve the Cloudflare request context.';

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if (process.env.NODE_ENV === 'development') {
errorMessage +=
'\n\n' +
dedent`
For local development (using the Next.js dev server) remember to include
a call to the \`setupDevPlatform\` function in your config file.
For more details visit:
https://github.com/cloudflare/next-on-pages/tree/3846730c/internal-packages/next-dev
` +
'\n\n';
}

throw new Error(errorMessage);
}

return cloudflareRequestContext;
Expand Down

0 comments on commit 90e140c

Please sign in to comment.