Skip to content

Commit

Permalink
avoid the default is not a function unclear error
Browse files Browse the repository at this point in the history
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>)
```
  • Loading branch information
dario-piotrowicz committed Aug 21, 2023
1 parent 4570aa6 commit b13ecd8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
19 changes: 19 additions & 0 deletions .changeset/brown-eagles-sparkle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
'@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

0 comments on commit b13ecd8

Please sign in to comment.