Skip to content

Commit

Permalink
Clarify error messages in endpoint routing.
Browse files Browse the repository at this point in the history
  • Loading branch information
lilnasy committed Feb 9, 2024
1 parent ef00224 commit c217ada
Showing 1 changed file with 9 additions and 2 deletions.
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

0 comments on commit c217ada

Please sign in to comment.