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

qol(routing): improve endpoint error message #10072

Merged
merged 2 commits into from
Feb 12, 2024
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
5 changes: 5 additions & 0 deletions .changeset/sweet-chicken-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Clarifies error messages in endpoint routing.
11 changes: 9 additions & 2 deletions packages/astro/src/runtime/server/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ export async function renderEndpoint(
)} requests are not available for a static site. Update your config to \`output: 'server'\` or \`output: 'hybrid'\` to enable.`
);
}
if (typeof handler !== 'function') {
if (handler === undefined) {
logger.warn(
'router',
`No API Route handler exists for the method "${method}" for the route ${url.pathname}.\n` +
`No API Route handler exists for the method "${method}" for the route "${url.pathname}".\n` +
`Found handlers: ${Object.keys(mod)
.map((exp) => JSON.stringify(exp))
.join(', ')}\n` +
Expand All @@ -38,6 +38,13 @@ export async function renderEndpoint(
// 404. Should be handled by 404.astro route if possible.
return new Response(null, { status: 404 });
}
if (typeof handler !== "function") {
logger.error(
'router',
`The route "${url.pathname}" exports a value for the method "${method}", but it is of the type ${typeof handler} instead of a function.`
);
return new Response(null, { status: 500 });
}

const response = await handler.call(mod, context);
// Endpoints explicitly returning 404 or 500 response status should
Expand Down
Loading