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

avoid the default is not a function unclear error #438

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
21 changes: 21 additions & 0 deletions .changeset/brown-eagles-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
'@cloudflare/next-on-pages': patch
---

avoid the default is not a function unclear error

if there is some issue evaluating a lazy loaded edge function
its default export ends up not being a function and that results
in an unhelpful error like the following:

```
TypeError: u.default is not a function
```

slightly improve the user experience by catching such error and
letting the user know that something went wrong with the edge
function's evaluation:

```
Error: An error occurred while evaluating the target edge function (<edge-function-path>)
```
15 changes: 14 additions & 1 deletion packages/next-on-pages/templates/_worker.js/utils/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,20 @@ export async function runOrFetchBuildOutputItem(
case 'function':
case 'middleware': {
const edgeFunction: EdgeFunction = await import(item.entrypoint);
resp = await edgeFunction.default(req, ctx);
try {
resp = await edgeFunction.default(req, ctx);
} catch (e) {
const err = e as Error;
if (
err.name === 'TypeError' &&
err.message.endsWith('default is not a function')
) {
throw new Error(
`An error occurred while evaluating the target edge function (${item.entrypoint})`,
);
}
throw e;
}
break;
}
case 'override': {
Expand Down
Loading